• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

working of System.out.println()

 
sapana jain
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.........

import java.io.*;

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
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you use Scanner since I find Scanner to be really easy.
[ April 15, 2008: Message edited by: annie roberts ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic