Answer :
Sytem is a java class. .. The
System
class contains several useful class fields and methods. It cannot be instantiated. out is a static PrintStream in the System class and println is a method of the printstream classExpaination :
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'? Since println() is clearly a method, and its called using 'out', then we know that 'out' can not possibly be a method because it doesn't make sense to have one method invoke another method with the dot operator in Java. This means 'out' must be a variable.
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.
Noticing the fact that 'println()' is clearly a method, we can further classify 'out'. 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.
The more exact answer is something like 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()'.
No comments:
Post a Comment