• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out.println()  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have looked at the Java API for the System and PrintStream classes and also read a few explanations on how this works, but I am just not able to reconcile this what I've learnt so far. I am not able to understand how System.out is an object/instance of the class PrintStream.

Here is what I understand about this:

System is a class
out is a static variable of type PrintStream in the System class

So, System.out makes perfect sense and I can understand this bit.

Then, println() is a non-static method in the PrintStream class. So, if something is using the "dot" operator to call the println() method, then it has to be an object of the PrintStream class. But I am just not able to figure out how System.out is an instance of the PrintStream class.

Please help me understand this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:
So, System.out makes perfect sense and I can understand this bit.

Then, println() is a non-static method in the PrintStream class. So, if something is using the "dot" operator to call the println() method, then it has to be an object of the PrintStream class. But I am just not able to figure out how System.out is an instance of the PrintStream class.

Please help me understand this.


Well, out is not an instance of PrintStream. It's a reference variable that points to an instance of PrintStream. Just like every other reference variable in Java.




Do you understand how Foo.bar is a reference variable that points to an instance of Date, and Foo.baz points to a String object? Then you understand System.out.

The actual line from the System class (at least in whatever version of Java I currently have) is:


Just because the variable is static, that doesn't mean it can't point to an instance of somethig. Since out is defined as static within System, that means it's associated with System as a whole, not with any particular instance of System. But it's still a reference variable so it still points to an object--an instance of PrintStream.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jeff. I don't understand this line. Is it supposed to be new PrintStream()?



And if it's new PrintStream(), then where is this line of code executed?

Thanks,
Prasanna
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Thank you, Jeff. I don't understand this line. Is it supposed to be new PrintStream()?


No, I copied it directly from System.java. It's some method that returns a reference to a PrintStream object. If you want to look at what it does, you can look at the code yourself. It's in src.zip in the JDK download.

But that detail is completely irrelevant. What we have is


That's all that matters. We have a variable. It points to an object. That's it. Nothing mysterious.

It is exactly like the Date and String lines in my example class.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jeff! I am now able to understand it. But now I get another question, from where can we ever call a method without using an object?

How can we just equate something to a method() just like that without using an object to call that method?

Also, when exactly does final static out get start pointing to the PrintStream object? When exactly is the line of code you wrote executed?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Thank you, Jeff! I am now able to understand it. But now I get another question, from where can we ever call a method without using an object?

How can we just equate something to a method() just like that without using an object to call that method?


You mean that ... = nullPrintStream(); line? It just means a method in the current class. If nullPrintStream() is static, it's equivalent to <my class name>.nullPrintStream(), so in this case, it would be equivalent to System.nullPrintStream(). If it's a non-static method, it's equivalent to this.nullPrintStream(), but since we're in a static context there, there is no "this", so it must be a static method.

Also, when exactly does final static out get start pointing to the PrintStream object? When exactly is the line of code you wrote executed?


When the class is loaded, all the static initialization lines and static initialization blocks are executed in the order they appear in the source file. The class is loaded the first time it is referred to in our code, although some core classes, like Object, Class, String, System, etc. are loaded by the JVM before our main() is ever called.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Jeff!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!