This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

understanding System.out.println();  RSS feed

 
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.
 
Rancher
Posts: 42975
76
  • 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: 42975
76
  • 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.
 
Java Cowboy
Sheriff
Posts: 16083
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: 42975
76
  • 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
 
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
 
Marshal
Posts: 59110
180
  • 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.
 
permaculture is giving a gift to your future self. After reading this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!