You are on page 1of 3

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 urelius !a "oman emperor# once said$ %&f each particular thing ask$ what is it in
itself' (hat 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.
(ith that in mind) let*s break this down) starting with the dot operator. +n 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. -ow) how do we categori.e ,out*' (ell) ,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. nd) ,out* does
not accept any arguments because only methods accept arguments you will never see
something like /0ystem.out!1)2#.println3. This means ,out* must be a variable.
(hat is /out3 in 0ystem.out.println!#'
(e 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.
4ecause ,out* is being called with the ,0ystem* 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. 0o now we know that ,out* is a
static member variable belonging to the 0ystem class.
+s /out3 in 0ystem.out.println!# an instance variable'

-oticing the fact that ,println!#* is clearly a method) we can further classify the ,out* in
0ystem.out.println!#. (e have already reasoned that ,out* is a static variable belonging to
the class 0ystem. 4ut 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. (ithout 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 0ystem.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.
(hen and where is the /out3 instantiated in 0ystem.out.println'
(hen the J5M is initiali.ed) the method initiali.e0ystem6lass!# is called that does
exactly what it*s name says it initiali.es the 0ystem class and sets the out variable.
The initiali.e0ystem6lass!# method actually calls another method to set the out variable
this method is called set&ut!#.
The final answer to how system.out.println!# works
The more exact answer to the original question is this$ inside the 0ystem class is the
declaration of ,out* that looks like$ ,public static final 7rint0tream out*) and inside the
7rinstream class is a declaration of ,println!#* that has a method signature that looks like$
,public void println!#*.
8ere is what the different pieces of 0ystem.out.println!# actually look like$
99the 0ystem class belongs to java.lang package
class 0ystem :
public static final 7rint0tream out;
99...
<
99the 7rinstream class belongs to java.io package
class 7rint0tream:
public void println!#;
99...
<

You might also like