Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

System.out.println()

  Asked By: Ashan    Date: Aug 07    Category: Java    Views: 848
  

In the statement:

System.out.println();
System is a class, and out is said to be a variable of which println() is a
method . I cannot understand this relationship.

Can variables have methods?
Can some one clarify me this please.

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Alfonsine Miller     Answered On: Aug 07

you declare variable  (normal variables, and
objects...) like this ? I am sure you do...


type name value
~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~
int i = 3;
PrintWriter out = new PrintWriter();


so every object is a variable of its class...
Similarly the variable(object) out is declared in
class System...

so we can access it like

System.out

but as it is an object so we can call any of its
method...

System.out.println();

got it... ?

 
Answer #2    Answered By: Fedde Bakker     Answered On: Aug 07

check out the javadoc for System. you'll see that out is a variable,
but it's a variable  of type PrintStream. PrintStream has the method
println()

so, variables  can have methods if the variable is an object with
methods, and not a primitive (int, float, byte, char, etc)

 
Answer #3    Answered By: Taylor White     Answered On: Aug 07

the return type of a vaible or a method could be set as class  Object return.

say

public static final String STR_SERVICE_NAME ;

public static final Thread THD_VARIABLE;

when i use THD_VARIABLE , i will get Thread class object.


if u 've doubt, plz. start implementing the same.

 
Answer #4    Answered By: Cay Nguyen     Answered On: Aug 07

here i sending u the example for ur doubts. check it out.
enjoy,

 
Answer #5    Answered By: Corbin Jones     Answered On: Aug 07

ok, first of all, do you understand  what the static keyword means? If
so, skip the following.

Static means that the class, method, or variable  may be called even if
an instance of that c/m/v has not been created.

Next, you must understand that you can string member/method calls
together. When this is done, the objects on the right are made more
specific by those on the left. For example, assume we have the
following class:

class A{
private int i, j;
public int getI(){return i;}
public int setI(int x){return i = x;}
}

if we have the following code

static void main(){
A a = new A();
a.j = 0;
a.setI(3);
System.out,println(a.getI());
}

What is being done?
1) create a new instance of class  A called 'a'
2) set the j member of 'a' to 0;
3) call the setI() method of 'a' and pass in the value 3
4) this is complicated, let's work this from the inside out.

First, a.getI() calls the getI function of 'a' and returns the value of i.

Now let's look at System.out.println().

-System is indeed a class.
-out is a static PrintStream object. As you might guess, it's used to
print a stream of characters. Since it is static, it doesnt need to be
created in your program.
-println() is a member of the PrintStream class.

So, the line System.out.println() calls the println() method of the
static member out of the System class. We can do this because out is a
static member of System and so does not need any instantiation.

Did this help at all?

 
Answer #6    Answered By: Taylor Evans     Answered On: Aug 07


System class  belongs to java.lang package.It provides platform independent
resources such as the standard input,output and error streams.All methods and
variables in system  class are public and static.due to this the class need not
be instantiated.

This java.lang package contains predefined input,output streams.

the basic printing methods are from printstream class.it provides references to
a printstream object that can access those methods

The out variable  is a member of the System class.

Since it is a Static variable to retrieve a reference to the out variable u have
to use the syntax

System.out.

 
Didn't find what you were looking for? Find more on System.out.println() Or get search suggestion and latest updates.




Tagged: