aspose file tools*
The moose likes Beginning Java and the fly likes Can someone please explain System.out.println Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can someone please explain System.out.println" Watch "Can someone please explain System.out.println" New topic
Author

Can someone please explain System.out.println

Kay Kingston
Greenhorn

Joined: Sep 04, 2012
Posts: 8
I know that System is a final class and it cannot be instantiated, out is a static final variable of type PrintStream in System class and println is a method in PrintStream class.Still I don't understand why we use System.out to call println() method.
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.
Are there any ways of calling a method apart from those I know(I have mentioned above what I know)?
Please help me....
Rakesh Keerthi
Ranch Hand

Joined: Jul 16, 2012
Posts: 144
Hi Kay,

This is a nice question.
System is static class from java.lang package(default package in java) and cannot be instantiated ,out is a reference variable defined in System also it is a final variable, println()/print() are the methods used to print on standard output. and these are oveloaded methods from PrintStream class.

Hope this helps


Kay Kingston
Greenhorn

Joined: Sep 04, 2012
Posts: 8
Rakesh Keerthi wrote:Hi Kay,

This is a nice question.
System is static class from java.lang package(default package in java) and cannot be instantiated ,out is a reference variable defined in System also it is a final variable, println()/print() are the methods used to print on standard output. and these are oveloaded methods from PrintStream class.

Hope this helps




Hey thanks for your reply but what all you said.... I do know and almost all I have said that in my query.
What I don't understand is the way it is being called System.out.println()(ie classname.variable.method).
Lets say I want to use hashcode() method of String class, what I do in my program is
Declare and Initialize a variable of String type like
String s="Hi";
and I say
s.hashcode();
right?
I didn't use and I suppose I can't use some classname.variable.hashcode();
And of course referring the JSE doc didn't help me
Will be very thankful if someone clears my doubt

Rakesh Keerthi
Ranch Hand

Joined: Jul 16, 2012
Posts: 144
Hi,
I tried an example corresponding to what you have written by creating 2 classes and using obj.hashcode() method, we have to use System.out.print() to print the data on console. the code i tried is as below.




and the output would be.

Hash code value is:611019154


please let me know if this is clear or you need anything else.
Apologies if anything is unclear as i'm also learning java newly.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Kay Kingston wrote:
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

you mean by this:
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You can create your own PrintStream and write to it. It's actually quite a common occurrence. You might do it to write to a file, write to an area of memory, write across the network etc.

But you want more than that here. You want a PrintStream that specifically writes to the console. That means it needs to write to standard out, and how that works is going to depend on the environment you're in (Unix, Windows etc). Helpfully, the JVM creates a PrintStream for you that's set up to write to standard out for your particular environment. And the JVM makes this available through a public static variable of the System class: System.out.



Kay Kingston
Greenhorn

Joined: Sep 04, 2012
Posts: 8
Punit Jain wrote:
Kay Kingston wrote:
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

you mean by this:


Yes that is what I meant but can you please explain System.out that you passed as an argument. Of course I know PrintStream doesn't have a no argument constructor, it has a constructor with OutputStream as an argument.And does the above code actually works?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Kay Kingston wrote:
Punit Jain wrote:
Kay Kingston wrote:
To my knowledge a method can be called using an object reference, in case of static behaviors we use classname. Then why here we are using System.out.println? Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

you mean by this:


Yes that is what I meant but can you please explain System.out that you passed as an argument. Of course I know PrintStream doesn't have a no argument constructor, it has a constructor with OutputStream as an argument


Matthew Brown wrote:
But you want more than that here. You want a PrintStream that specifically writes to the console. That means it needs to write to standard out, and how that works is going to depend on the environment you're in (Unix, Windows etc). Helpfully, the JVM creates a PrintStream for you that's set up to write to standard out for your particular environment. And the JVM makes this available through a public static variable of the System class: System.out.

Kay Kingston wrote:
And does the above code actually works?

try this.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

The default implementation when you write System.out.println("..."); is to write to the system console. If you want your own implementation as to how and where the output should go, you have to mention your own OutputStream and call the above-mentioned or other PrintStream constructor. A simple example at this link.
Can't we just create an object of PrintStream class and call the println() method as PrintStream class can be instantiated.

In that case the default implementation is gone. You have to, say for example, create a file, then pass it...You can try some code to check what all you can do!

The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Kay Kingston wrote:


Yes that is what I meant but can you please explain System.out that you passed as an argument. Of course I know PrintStream doesn't have a no argument constructor, it has a constructor with OutputStream as an argument.And does the above code actually works?

It works (try it yourself), but it doesn't do anything useful.

The PrintStream constructor can take any Stream. It "wraps" the Stream you pass to it - the idea is that you can write to the same location as that Stream, but using the interface that PrintStream provides (which is very convenient for writing text).

Since a PrintStream is-a Stream, you can pass it as the constructor argument to a new PrintStream. But all that gives you is a PrintStream that writes to the same location as the original PrintStream. In other words, it does exactly the same as the object you started with. So you haven't gained anything.
Pallavi Sadit
Ranch Hand

Joined: Jul 24, 2014
Posts: 36
    
    3
Hi Kay,
The variable out in the System class is initialized with the default output stream object at the time of JVM startup. So, System.out is actually returning a PrintStream object on which you can call any method of PrintStream including println(). Wherever you need to use the default output stream (console) to print your data, you can directly call the System.out.println() , wherever you need to use an output stream of your choice like a file, you can instantiate a PrintStream object with that output stream and use it .
 
jQuery in Action, 2nd edition
 
subject: Can someone please explain System.out.println