Question

In the following link, the author states that

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()’.

But how can a class variable, i.e. static variable, be an instance variable as well? It states that it is an instance of a class, which is true - without the class, out ceases to exist. But it isn't an instance variable because there's only one out for every instance of System objects. (Note that System owns private constructors.)

So was ProgrammerInterview wrong?

Était-ce utile?

La solution

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.

Autres conseils

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

Your confusion comes from the fact that this sentence is a little bit imprecise (is not wrong, just sloppy language). More precise would be:

But now we can see that ‘out’ must contain a reference to an instance of a class

(and the class which is meant here is PrintStream, not System).

The author is not wrong, as there's the difference between an object instance and an instance variable as outlined in the other answer.

However the author is wrong about how one determines that out is an instance variable. The author writes:

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...

But here's a quick counter example to show it's not that easy, where my out is a class.

public class JavaTest
{
    public static void main (String[] args)
    {
        Mysystem.out.println("Hello world");
    }

    static class Mysystem 
    {
        static class out
        {
            static void println(String s)
            {
                System.out.println(s);
            }
        }
    }
}

The other answers cover the correctness of the text of the article. But you might have been confused, because the article's section heading is incorrect. In the section titled What is “out” in System.out.println()?, they determine that out is not an instance variable. The next section is titled Is “out” in System.out.println() an instance variable? But they just answered that! No need to ask again. And they don't question this in this section. Instead, this section examines if out is "an instance of a class". This makes it look as if "instance variable" means the same as "instance of a class", because they wrote one when they meant the other. But it's a typo, not a synonym. They probably meant to title that section Is “out” in System.out.println() an instance?

Licencié sous: CC-BY-SA avec attribution
scroll top