• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why out object is combined with System class like - System.out.print()?  RSS feed

 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print method belongs to PrintStream class in java and its "out" object is referenced in System class...Why...?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"out" is a static member of the System class.
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sir I know but the query is why is doing stuff with System class..?

It could be used with directly PrintStream....
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing stopping you creating your own PrintStream to write to - it's what you do if you want to write to a file, for instance. But you don't want to write to any old stream, you want to write to a specific PrintStream that writes to the standard output of the operating system. System.out is how Java gives you access to that stream. The System class tends to contain things that are related to the environment the code is running in.
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any code sample for using out object with some another class...?

Is "out" object also the member of some another class....?

Describe please....

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out is a static variable of the System class that references a PrintStream object that writes to standard output. out is always a PrintStream, but it's not the only PrintStream.
 
Bhavesh Kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it... Thanks a lot ||

also describe the functionality of PrintStream class.
Is it the only class to print on the console..?
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhavesh, check the sample code below. For info, refer to jdk doc.



Regards,
. ^_^
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhavesh Kumar wrote:also describe the functionality of PrintStream class.
Is it the only class to print on the console..?

Was there a "please" missing here?

PrintStream does not in general have anything to do with the console - a PrintStream can print anywhere a stream connects to. It all depends on how you set up the PrintStream. If you're using System.out, that is (by default) a PrintStream that connects with the console. But most other PrintStreams have nothing to do with consoles.

If you want to learn about PrintStream in general, try clicking on the link that appears here when you write the word PrintStream. <= (that's a link)
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you're interested in writing or reading from the console, check out java.io.Console <= (That is also a link)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!