Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do we able to access System.out.println(); inside java program?  RSS feed

 
Sarvya padwal
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System class is in lang package.
and we know, To access class members we require object of that class.
and we cant create object of System class.
then How do we able to access System.out.println(); inside java program?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66182
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clue: static.
 
Andy Jack
Ranch Hand
Posts: 257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Clue: static.


Yes, Sarvya please see the oracle java documentation.

java.lang.Object
--extended by java.lang.System

So, we don't need an object of System class to access the out field/variable which is static.

Take a look at the "signature" of the out field - public static final PrintStream out
Notice that its public (and static) , which is why you can access it from anywhere.
That won't be the case if it was private, protected or nothing/package.

Sarvya , you also said that - "To access class members we require object of that class"
I'd like to add to that. This is also how you can access member variables of a class -
1 - If they are public and you can get an object of that class.
2 - If the member variable is private, then we need member methods such as getters and setters that let you get and set that member variable.

As an aside, we see the error "The constructor System() is not visible"
for the code below.

 
Sarvya padwal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!