How System.out.println() works

In Java, how does System.out.println() work?

This question is an excellent example of how just some very basic knowledge of Java can lead you to the correct answer. Most interviewers would not expect you to know the answer to do this right away – but would like to see how you think and arrive at an answer.

Marcus Aurelius (a Roman emperor) once said: "Of each particular thing ask: what is it in itself? What is its nature?". This problem is an excellent example of how that sort of thinking can help one arrive at an answer with only some basic Java knowledge.

With that in mind, let’s break this down, starting with the dot operator. In Java, the dot operator can only be used to call methods and variables so we know that ‘out’ must be either a method or a variable. Now, how do we categorize ‘out’? Well, ‘out’ could not possibly be a method because of the fact that there are no parentheses – the ‘( )’ – after ‘out’, which means that out is clearly not a method that is being invoked. And, ‘out’ does not accept any arguments because only methods accept arguments – you will never see something like “System.out(2,3).println”. This means ‘out’ must be a variable.

What is “out” in System.out.println()?

We now know that ‘out’ is a variable, so we must now ask ourselves what kind of variable is it? There are two possibilities – it could be a static or an instance variable. Because ‘out’ is being called with the ‘System’ class name itself, and not an instance of a class (an object), then we know that ‘out’ must be a static variable, since only static variables can be called with just the class name itself. So now we know that ‘out’ is a static member variable belonging to the System class.

Is “out” in System.out.println() an instance variable?

Noticing the fact that ‘println()’ is clearly a method, we can further classify the ‘out’ in System.out.println(). We have already reasoned that ‘out’ is a static variable belonging to the class System. But now we can see that ‘out’ must be an instance of a class, because it is invoking the method ‘println()’.

The thought process that one should use to arrive at an answer is purposely illustrated above. Without knowing the exact answer beforehand, you can arrive at an approximate one by applying some basic knowledge of Java. Most interviewers wouldn’t expect you to know how System.out.println() works off the top of your head, but would rather see you use your basic Java knowledge to arrive at an answer that’s close to exact.

When and where is the “out” instantiated in System.out.println?

When the JVM is initialized, the method initializeSystemClass() is called that does exactly what it’s name says – it initializes the System class and sets the out variable. The initializeSystemClass() method actually calls another method to set the out variable – this method is called setOut().

The final answer to how system.out.println() works

The more exact answer to the original question is this: inside the System class is the declaration of ‘out’ that looks like: ‘public static final PrintStream out’, and inside the Prinstream class is a declaration of ‘println()’ that has a method signature that looks like: ‘public void println()’.

Here is what the different pieces of System.out.println() actually look like:

//the System class belongs to java.lang package
class System {
  public static final PrintStream out;

//the Prinstream class belongs to package
class PrintStream{
public void println();

What do you think?

  • Dasharath

    Why we are using println() method in System class??. why can we call directly from PrintSream class. Is there any advantage calling the println() in Sysytem class?

  • Robert Harvey

    Related discussion occurring here:

  • Gaurav Rawat

    Since out variable is final as stated (public static final PrintStream out) how does setOut() method works available in System class?

  • Sam

    println() is obviously a non static method, not because of System.out.println() expression alone, but because println() can be used with any PrintStream instance, and can behave differently for every instance (ex print to screen, print to file, etc).

  • Arashi

    Well, let’s work backwards. Say println() was a static method. That would immediately imply that “out” was a class, despite beginning with a lowercase letter. Perhaps some kind of static nested class in System? Very unusual.

    On the other hand, if println() is nonstatic, then “out” must be an object, i.e. an instance of some unknown class. Considering the fact that “out” begins with a lowercase letter, surely that makes much more sense?

  • okeyxyz

    “…we can see that ‘out’ must be an instance of a class, because it is invoking the method ‘println()’…”

    The above does not make sense. How do we know that “println” is not a static method? Remember that we did not know that “out” was an instance of PrintStream before you made the above statement. So how doo we know that “out” is an instance of PrintStream then? off the top of our head then?

  • Saif

    Why to make out variable as final?

  • William

    Assume one doesn’t know the answer, one can also deduce that Out is an inner class of System class and printIn() is a static method of class System.Out. There is no fault in this reasoning even though it is not the correct answer.

  • Shivam Biswas

    out is the ref of printstream class which is avilable in pakg n system class is belongs from java.lang it(printstream class) maks avilable to System class…i wannna knw plz hlp with detail elabratn..where we can see in system class its nt importing pakge

  • Nandeesh

    how will you say that out is an object? because i cannot find creating an object ‘out’..

  • llw

    In Java API,System class is in the package of ‘java.lang’,the method ‘println()’ is belong to the class of PrintStream,PrintStream class is in the package of ‘’, so I can’t agree your explanation.

  • Gopinath M B

    Good explaination…I never thought about ‘out’ this much before 😛

  • Rock

    Why not?. can’t a Instance variable or static variable call a method?

  • Haveesh

    Out is a object of PrintStream class that resides in System class. So System.out.println() is correct…

  • MichaelM

    The questions from others is a reason why these types of questions seem irrelevant to me. Maybe when would you use the command or what is it used for? I wonder how many actual programmers could articulate something like this versus use it adequately? If you’re basing hiring someone on these types of questions I’d ask if you’re hiring a programmer or a professor? lol

  • System class uses static final reference variable ‘out’ ( public static final PrintStream out; ) of type PrintStream to invoke the method ‘println()’ defined in PrintStream class. Actually System class imports '' package to so that it can invoke the method ‘println()’

    You can view : System.out.println() explained for better understanding.

  • hamreen ahmad

    ‘out’ is an object of the reference type PrintStream

  • Guest

    The question is how is System.out connected to println(). Are you saying it’s a variable that calls a method. I think that’s the puzzling part.

  • visitor

    I was wondering how the hell can we call a non static println() method without any instance, thanks for the info about initializeSystemClass().

  • V

    System is public final and PrintStream is public.

    And PrintStream has println method definition, not just the declaration. Actually there are multiple println overloaded method definitions there.

    println() calls print() which in turn calls write() and then the calling chain goes like that to print the parameter passed.