• Post Reply Bookmark Topic Watch Topic
  • New Topic

understanding System.out.println();  RSS feed

 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


how is that we are able to use 'println()' with 'out' while 'out' is not pointing to any PrintStream object or anyobject(in javadocs).
though 'out' is just a static reference variable which is not pointing to any object.

I hope my question is understandable.

Thank you.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static reference is still a reference - it most certainly points to an object.
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry sir i did not get you.
do you mean a static reference varaible implicitly points to its own object?
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'out' is a static field in System class which is not pointing to any object means we must get null pointer exception when we do System.out.println();
right?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Why do you think "out" is not pointing to an object? As I said, it absolutely does. Whether the reference is static or not makes no difference to that.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misunderstand what static means exactly.

If a class has a static member variable, like the variable out in the System class, then that means that there is only one copy of the variable which is shared by all instances of the class System. Compare that to a non-static member variable: in that case, each instance of the class System would have its own copy of the variable out.

So, the static member out is not associated with any particular System object.

That does not mean that out does not refer to a PrintStream object.

To read more about what static means exactly, see this page in Oracle's Java Tutorials: Understanding Class Members
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so it is pointing to an object.ok
but where do I get to see the (System)class structure?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by "class structure" - the javadocs will tell you which methods and fields it has: java.lang.System
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As @Ulf said why don't you check System class source code , there you get to know how out is initialized.
 
Gajendra Kangokar
Ranch Hand
Posts: 94
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes the declaration in src code is



Thanks all for reply
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

I am suspicious about the System class. It is possible that the JVM initialises some of its fields at runtime, using a different mechanism from the ordinary Java® code. I do not know whether that happens, but if it does, the System class behaves differently from its code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!