File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes the mystery of System.out.println().........:confused: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the mystery of System.out.println().........:confused: " Watch "the mystery of System.out.println().........:confused: " New topic
Author

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

nick kaushik
Ranch Hand

Joined: Sep 25, 2009
Posts: 48

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


"ye shall know the truth & the truth shall set you free..."
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
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...)


OCJP 6 (93%)
Stefan Brandenberger
Greenhorn

Joined: Aug 31, 2009
Posts: 12
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

Joined: Sep 25, 2009
Posts: 48



what's that?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
s.palanivel rajan
Ranch Hand

Joined: Sep 22, 2009
Posts: 40
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..


With Regards,
S.Palanivel Rajan B.E.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 597

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.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Vivek Singh
Ranch Hand

Joined: Oct 27, 2009
Posts: 92
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

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....

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: the mystery of System.out.println().........:confused:
 
Similar Threads
Method Local Inner Class
Local Inner class
System.out.println()
final loacl variables
Object Creation