Most interviewers don’t expect the correct answer for this question. But this question is asked to know about the candidate’s knowledge about Java and their thinking ability. This question is a sample for how you can solve a problem with some fundamental dogma in Java. In Java, dot operator is only used to call variables and methods. So you can assume “out” is conversely a variable or a method. Since there is no parentheses () is used after “out” it is safe to assume that “out” is not a method so ultimately out is a variable. Also “out” is not accepting any arguments and only methods will accept argument. One will never witness an entity like System.out(9,7).println. This concludes “out” is a variable not a method.
- Related: All Java interview questions
What kind of variable is “out”?
After coming to a conclusion that “out” is variable, now we must classify what kind of variable is “out”. The variable are of two kinds either it is static or instance. Since out is called along with system class, we can come to a conclusion that out is a static variable because only static variable can be called along with system class. Now we know that out is a static variable which is called along with the system class.
Is “out” an instance of class?
It is now clear that “out” is a static variable which belongs to the system class and println() is a method. We have already classified that out is a static variable but “out” is calling the method “println()” so the static variable “out” must be an instance of class. The variable “out” is also a type of printstream.
When does the static variable “out” is instantiated?
The system is a class which was stacked into the kernel of Java Virtual Machine architecture. This class also have a modifier and contains the methods and field. These methods and fields provide standard I/O options. When initializesystemclass () is called it initializes the system class and set the variable “out”. Also the initializesystemclass () will call another method to set the static variable “out”. This process of setting the variable out is known as setout().
How exactly the System.out.println() works?
In system class, the denunciation of the variable “out” will look like “public static final printstream out” and in printstream class acts as a declaration of println(). The method signature of println() is public void println(). The multiple printing process of println can be called with several arguments.
Sample java program is,
//Method is following
System.out.println(“Please Enter any integer number here:”);
int firstIntNumb = userInput.nextInt();
System.out.println(“Please Enter second integer number here:”);