• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

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

 
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?
 
Sheriff
Posts: 67265
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clue: static.
 
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!