I am just a newbie in Java. I was wondering the way System.out.println()
is used. Out is a static field inside System
class. The type of out
is PrintStream
. But when I saw the constructor of PrintStream
class, it takes a parameter of type OutputStream
and as far as I know we cannot create the object of an abstract class. In that case we must pass some subclass's object to the constructor of PrintStream
. What is that class? Same is the System.in
. It is also InputStream
's reference but what is the type of object it points to as the InputStream
is abstract?

- 1,848
- 2
- 30
- 60

- 397
- 6
- 18
-
You can use java's reflection to check all important object information at runtime. – Dariusz Jan 21 '13 at 08:51
-
2Try `System.out.println(System.out.getClass());` or view it in your debugger. ;) – Peter Lawrey Jan 21 '13 at 08:52
-
1@PeterLawrey this will print `class java.io.PrintStream` nothing else :) The idea with debugger is more efficient ;) – Andremoniy Jan 21 '13 at 08:53
4 Answers
PrintStream
wraps BufferedOutputStream
, which wraps FileOutputStream
, which is writing into the console, which has its own FileDescriptor
.

- 34,031
- 20
- 135
- 241
-
I'd like to add that the static field System.out does not have to be instantiatied by the user. It can be just used. My feeling is that the OP is having some trouble with understanding that. – Fildor Jan 21 '13 at 08:54
-
@Fildor I have not that feeling. It seems to me, that it is just theoretical question. – Andremoniy Jan 21 '13 at 08:59
-
1Technically it is a `FileDescriptor`. The `File` class is not used. – Peter Lawrey Jan 21 '13 at 09:04
-
1
A simple way to view the structure of a class is to examine it in a debugger.
As you can see @Andremonify's description is basically what you have.
FileDescriptor
- 0 is System.in
- 1 is System.out
- 2 is System.err
- 3+ is used for other files

- 525,659
- 79
- 751
- 1,130
-
`FileInputStream` for `stdin`, `FileOutputStream` for `stdout`. What about `stderr` – overexchange Oct 26 '17 at 11:50
-
-
Correct. Can you also comment on first part of [query](https://stackoverflow.com/q/46953036/3317808)? about the idea of stream – overexchange Oct 26 '17 at 21:19
Yes out
is of PrintStream
type. And constructor of PrintStream
takes OutputStream
type. OutputStream
is abstract class. But any superclass refrence can refer subclass object without casting, so PrintStream's constructor has OutputStream
refrence, but this refrence must be referring one of OutputStream
's subclass like FileOutputStream

- 3,320
- 7
- 35
- 68
There are a couple more things to say about the implementation of System.out
.
The actual implementation class of
System.out
is not specified. The javadocs don't say what it is. We observe (in various way) that Oracle Java and OpenJDK Java implement the "stack" in a particular way (see other answers), but this could change in the future.The
System::setOut(PrintStream)
method can be used to modify whatSystem.out
is bound to. If that happens, any assumptions about implementation classes may be incorrect.It turns out that you can do this:
System.setOut(null);
so
System.out
could benull
. However, with current implementations,System.out
won't benull
unless you set it tonull
.
All that is actually guaranteed by the specifications is that the value of System.out
will have a type that is assignment compatible with PrintStream
.

- 698,415
- 94
- 811
- 1,216
-
But we know the implementation class is either PrintStream or a subclass of PrintStream, right? – Bernhard Barker Oct 26 '17 at 11:57
-
Yes. That's more or less what "assignment compatible" means! (But ... quibble ... `null` does not have an implementation class. Which is why "assignment compatible" is technically more accurate than what you said.) – Stephen C Oct 26 '17 at 12:03