Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

about system.in & system.out  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
when i read a book it was given that "in" & "out" in system.in & system.out are variables but we can acess methods only through objects so how the method print() is called by the variables in & out.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in and out are variables that reference objects.
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods are static methods, and therefore do NOT require an object in order to use them.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kanagaraj,
Let me try to explain a little bit on this. System class contains the static fields
public static final PrintStream out
public static final InputStream in
public static final PrintStream err
which refers to the standard output, input and error streams. note that these fields are declared as static and hence can be accessed without instantiation.
print() and println() are methods defined in the PrintStream class. out being defined as Printstream type can access println() using out.println().
Hope this is clear.
Kalidas
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me cases of when you would use each of these static methods?
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalidas I think you'll find they are not final. There are setters for them so you can plug in your own.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Stan James:
I think you'll find they are not final.



Ah. This is one of the hairy little corners of the Java API. They are final -- go check! The setOut(), setIn(), setErr() methods delegate to native methods, which aren't bound by ordinary mortal Java rules. Final variables which can change -- in the API! Aaaak!

A better implementation seems obvious to me: PrintStream isn't final, so they could have just used a package-private PrintStream class which allowed the underlying OutputStream to be changed, and then have setOut() merely twiddle that internal setting, without this crazy final variable modification.

Never did figure out their rationale there.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
The methods are static methods, and therefore do NOT require an object in order to use them.



The print() and println() methods are NOT static, but as mentioned above, you call them via static reference variables. This means that you are calling these methods on objects just as required for non-static methods.

Layne
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!