• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out.println() doubt-  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we use System.out.println() for console output
now System is a class out is an object and println() a method.
Here we are using class.object(or reference).method() name to call a particular method.

can we call for any other class for example if i have a class A having an object a( A a=new A() )and a method say show() then why we dont use
A.a.show() to call show()

However it is done in System.out.println() case.why?
 
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call class.object because System's members are public static members. Think of System as a utility class. You don't want to instanciate it (actually you can't), so you just access its members and its methods directly, because they are static.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh,

f i have a class A having an object a( A a=new A() )and a method say show() then why we dont use
A.a.show() to call show()



You can do that if a is static and gets instanciated in the A class.
Example:






Yours,
Bu.
[ October 10, 2006: Message edited by: Burkhard Hassel ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Try and understand the use of static java keyword.
2. There is a small difference between static fields and non-static fields of a Class.
2.1 If Class A has a non-static field a1 and then you create 10 objects of class A, each object has a field a1 in it and they can all hold different values.
2.2 If Class A has a static field a2 and if you load the class A in memory( you haven't created any object of it, yet), a2 is available to be accessed. Now if you create 10 objects of Class A, those objects DO NOT have the field a2. There is only one copy of a2 in memory and it can be accessed using A.a2. Even if you access a2 via one of those 10 objects you are still acessing the same a2.
3. So, out is a static field and hence it is accessed as System.out. println() just happens to be a method of Class Out and hence can be run as out.println().
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by BHARAT CHHAJER:

3. So, out is a static field and hence it is accessed as System.out. println() just happens to be a method of Class Out and hence can be run as out.println().


Small correction: "out" is a static member of class System; it is of type java.io.PrintStream (not "Out").
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way is:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bauke Scholtz:
Another way is:


Another way to call this particular method, yes; but completely irrelevant to this discussion, I'm afraid.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be interesting for the topic starter to know how to access non-static methods anyway without the need to declare. And whether being relevant or not is up to the topic starter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!