What variable is being referred to here?

142 Views Asked by At
class Test {
  int a;

  void method() {
    a = 1;
    int a = a = 2;
    a = 3;
  }
}

There are lots of as in method. What are they all referring to?

1

There are 1 best solutions below

3
On

This is a simple example of the bizarreness of Java's scoping rules.

a = 1;
int a = a = 2;
a = 3;

Breaking it down line-by-line:

  • a = 1; is referring to the member variable.
  • a = 3; is referring to the local variable, because it's after the declaration of the local variable. It's pretty confusing that you can refer to two different symbols via the same identifier, in the same method.
  • int a = a = 2;: the second a is the local variable.

The self-reference in the variable declaration is really curious. You can find this in the language spec:

  • The scope of a local variable declaration in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any further declarators to the right in the local variable declaration statement.

It is also true that member variables can refer to themselves in their own initializer; but this is for a slightly different reason:

  • The scope of a declaration of a member m declared in or inherited by a class type C (§8.1.6) is the entire body of C, including any nested type declarations.

I have yet to find a compelling reason for the existence of this rule for local variables; maybe you just really really want to make sure it's assigned. The only thing that I can think it allows you to do is to use the variable as temporary storage in the evaluation of the initializer, for example:

int a = method(a = somethingThatIsReallyExpensiveToCompute(), a);

would be equivalent to:

int a;
{
  int tmp = somethingThatIsReallyExpensiveToCompute();
  a = method(tmp, tmp);
}

Personally, I'd rather see the second form of the code, as the evaluation of the first just seems obscure.