So was ProgrammerInterview wrong?
No. It is correct.
It says:
"But now we can see that out
must be an instance of a class ..."
That is not saying that out
is an instance variable. An instance variable is a variable that belongs to an instance, NOT a variable that contains (refers to) an instance.
However, I think that there is a flaw in the logic of this statement:
But now we can see that ‘out’ must be an instance of a class, because it is invoking the method ‘println()’.
... when referring to this:
System.out.println();
I read this as saying that System.out.println()
is the syntax for invoking an instance method, and therefore the value of System.out
must be an instance of some class.
It is true that System.out
does refer to an instance, and println()
is indeed an instance method. But this does not follow logically from the syntax.
Why? Because you can use the exact same syntax to invoke a static
method. (This is specified in JLS 15.12 ... but it is a long and complicated read.)
Example:
public class Test {
private static void jello() {
System.out.println("jello world");
}
public static void main(String[] args) {
// The preferred way to invoke a static method
Test.jello();
// This also works ...
Test test = new Test();
test.jello();
}
}
Therefore, we cannot deduce from the syntax used that an instance method is being invoked.