my dog learned polymorphism
The moose likes Beginning Java and the fly likes working of 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 "working of System.out.println()" Watch "working of System.out.println()" New topic

working of System.out.println()

sapana jain
Ranch Hand

Joined: Oct 24, 2007
Posts: 42
I was just searching on google for the same and I came across the following code but I am confused specially with this line........

Because “out” in “System.out” is a static PrintStream object, you can create a PrintStream variable, assign “System.out” to it, and reference that object from the new variable instead.
so now please tell me how can we have a static object?
The code is as follows.........


public class TestPrint
private static PrintStream out = System.out;

public static void main(String args[])
out.println(“Hello, World!” ;
out.println(“Testing, one, two, three” ;
annie roberts

Joined: Apr 14, 2008
Posts: 12
Why don't you use Scanner since I find Scanner to be really easy.
[ April 15, 2008: Message edited by: annie roberts ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Go through the standard API, find System, here, read about out and setOut. Also find out about PrintStream.

If you can set up a PrintStream object with a file in, for example, you can use the setOut method to tell System to use that as "out." So whenever you call "out" it will write to your file instead of the terminal window.
f. nikita thomas
Ranch Hand

Joined: Mar 02, 2008
Posts: 87
look at the following:

to create (instantiate) an object we use the new keyword. so ...

now it exists and hmmm, is it static? you tell me ...

Imagination is more important than knowledge "Albert Einstein"
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Is there such a thing as a static object at all? I don't think there is.
What you have is a second reference which you called out, whihc you have made point to the same object as System.out. Your reference happens to be static in that instance. I can't see any point in having two "out"s which both point to the same thing . . .

Please use code tags around quoted code. The presence of the s suggests you have an additional ; before the ) which will probably show up as a compiler error.
I agree. Here's the link:
subject: working of System.out.println()
It's not a secret anymore!