1

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?

Robert Harvey
201k55 gold badges469 silver badges682 bronze badges
asked Jun 12, 2016 at 5:07

4 Answers 4

6

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.

answered Jun 12, 2016 at 5:10
5
  • If it's an instance of a class, then that means that for out to exist, a class must exist also. But not necessarily an instance. Yes or no? Commented Jun 12, 2016 at 5:19
  • No. Necessarily an instance ... unless System.out is null, which it never is. Commented Jun 12, 2016 at 5:44
  • All non-null non-primitive variables in java contain an instance of a class, that is, an object. Such variables can be stored in static class space, in other instances on the heap, in the stack as a method argument, and in a fortune cookie that has disturbingly intimate knowledge of what's stored at memory locations in your computer. Where they are stored and what they contain are independant of each other. Commented Jun 12, 2016 at 12:44
  • Compilers warn that test.jello() is a static call using instance syntax (may require configuration). Commented Jun 13, 2016 at 4:04
  • @Basilevs - That is correct. And you >>shouldn't<< call static methods that way. But it doesn't follow that people won't do it. The inference is incorrect. Commented Jun 13, 2016 at 14:01
2

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

answered Jun 12, 2016 at 8:07
1
  • Probably a less convoluted way of saying this would have been "out cannot be a primitive type, because we're invoking a method from it"? Commented Jun 12, 2016 at 14:33
0

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);
 }
 }
 }
}
answered Jun 12, 2016 at 6:38
2
  • 1
    Huh? What, exactly, are you trying to convey? I'm guessing that you're trying to show that dots are also used in qualified classnames, but that certainly wouldn't be apparent to someone that doesn't already know the language. And perhaps it isn't what you're actually trying to convey. A better edit will remove the -1. Commented Jun 12, 2016 at 11:59
  • @kdgregory Well, it's clear to me: in Java, the dot is not enough to classify an identifier as a method or a variable, because it could also be a class. Commented Jun 12, 2016 at 14:36
0

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?

answered Jun 12, 2016 at 13:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.