• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the mystery of System.out.println().........:confused:

 
nick kaushik
Ranch Hand
Posts: 48
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys..
my question is about System.out.println()....... Now System is a class we know that... out is a variable & println()/print() are methods.......how is it possible that class name & variable are both used to call a method?can somebody please explain this how is it happening?
thanks in advance
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick kaushik wrote:how is it possible that class name & variable are both used to call a method?can somebody please explain this how is it happening?


It's sorcery!

(Go get a java/OOP book and read about static class members...)
 
Stefan Brandenberger
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at class System:



So "out" is a public static field of type PrintStream in the System-class. And with System.out.println() you invoke the method "println()" on this PrintStream-Object.
 
nick kaushik
Ranch Hand
Posts: 48
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what's that?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a method (probably a private method) in class System.

Note: You can take a look at the source code of class java.lang.System yourself; you'll find it in the file src.zip which is in your JDK installation directory.
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey nick..,

Its easy man don't get confused.. System is the class name and println is the method defined in system class though it is declared as static we are using system.out to call that method...is it clear..
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s.palanivel rajan wrote:Hey nick..,

Its easy man don't get confused.. System is the class name and println is the method defined in system class though it is declared as static we are using system.out to call that method...is it clear..


Well - I don't think that is technically (or logically) correct. Let me spell it out,

System is a class.
out is a static variable of type PrintStream in the class system.
println is a method of the PrintStream class (and its not static).

System.out.println() - is a convenient way to write output to the stdout rather than creating your own output stream every time you had to display some output.
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick kaushik wrote:hi guys..
my question is about System.out.println()....... Now System is a class we know that... out is a variable & println()/print() are methods.......how is it possible that class name & variable are both used to call a method?can somebody please explain this how is it happening?
thanks in advance

Just look at the signatures...
is an object of java.io.PrintStream
Where as
 
salvin francis
Bartender
Posts: 1307
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets try that in a simple example:



Check and see if

works.


sorry guys if i made a typo, i didnt compile the code before posting....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic