• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

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

 
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • 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
 
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...)
 
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
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what's that?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
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..
 
Ranch Hand
Posts: 607
Firefox Browser Spring Java
  • 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.
 
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
 
Saloon Keeper
Posts: 2741
133
Google Web Toolkit Eclipse IDE 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....
 
reply
    Bookmark Topic Watch Topic
  • New Topic