Freeman Lou
Freeman Lou

Reputation: 139

In Java, how does System.out refer to PrintStream class?

I'm a beginner learning Java with some knowledge of C++, and the System.out.println(); is confusing me right now. So System is the class, out is a variable that can call a method?? According to: http://journals.ecs.soton.ac.uk/java/tutorial/getStarted/application/objects.html out is a class variable, and a variable is a storage location in the computer memory that has a type name and content. It's not an object like string that can use methods like .getLength(). The way the website explains it is that out refers to an instance of PrintStream class, but how?

Upvotes: 4

Views: 7355

Answers (8)

Jyotsana Nandwani
Jyotsana Nandwani

Reputation: 198

Think of System class roughly like this :

package java.lang;
public final class System {

    public final static PrintStream out;
}

here out is a static final variable of "type" Printstream. Since it is a static variable, we can call it by "ClassName.variableName" without creating any object of System class, so we do System.out.

Now, out is a reference variable of "PrintStream" class. Till now, only this reference variable is created and it is not referring to any "object" of Printstream class. But System class creates object of PrintStream when it is loaded in memory. For this, see methods initializeSystemClass() and setOut0() in below link which is complete source code of System class. (Dont be overwhelmed by this enormous code, just be assured that there is "new PrintStream()" called inside System (here at line 1095)).

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/System.java

so, when we have an object of PrintStream class, our out reference variable can easily call a method on it, right? This is how we call System.out.println()

Upvotes: 0

Varunteja Jonnala
Varunteja Jonnala

Reputation: 3

Actually the out parameter in System.out.println(String args[]) is a static field in System class. Whenever a field is declared it should have certain datatype. In this class the out field is defined as static PrintStream out;.

It means the datatype of out is PrintStream class.In this way System.out will actually represents a object of PrintStream class. With this object we are calling println() method of PrintStream class.

Upvotes: 0

Priyam
Priyam

Reputation: 69

System is a final class which has a final variable out holding a object of PrintStream class on which we can call println() method.

Upvotes: 0

NPE
NPE

Reputation: 500267

It's not an object

This is where your reasoning is going wrong. System.out is (a reference to) an object.

The type of the reference is PrintStream, as documented in the Javadoc. This means that you can call PrintStream's methods on System.out, e.g.:

System.out.println();

Upvotes: 6

jahroy
jahroy

Reputation: 22692

Out is a public static field of the class named System.

Because it's public, you can call methods on it.

Its type is PrintStream.

The best way to learn is to read the documentation:

Notice the part at the top where it describes in, out, and err.

Upvotes: 1

Dmitri
Dmitri

Reputation: 9157

More strictly, it's a public static field that is a reference to an object of type PrintStream, so yes, you can call methods on it.

Java references are roughly analogous to C pointers (at least in the way they are used, obviously there are significant differences).

Upvotes: 1

thedayofcondor
thedayofcondor

Reputation: 3876

System.out is a particular instance of PrintStream, whose output is linked to the equivalent of the C++ stdout

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382122

out doesn't call a method : out is a variable holding an object (an instance of PrintStream) on which you can call a method.

For example :

System.out.println("hey!");

You could also do

void print(PrintStream ps, Object o) {
    ps.println(o);
}
...
print(System.out, "hey!");

Upvotes: 1

Related Questions