Is `this` optional in static inner classes in Java?

Viewed 33

The following example has two static inner classes - both appear to behave the same. Is the this variable just implicit in the second - why isn't it necessary? Why does this compile?

public class MyClass {
    public static void main(String args[]) {
      ImplicitThis implicit = new ImplicitThis(10);
      ExplicitThis explicit = new ExplicitThis(10);

      System.out.println("Implicit x value = " + implicit.x + ", Explicit x value = " + explicit.x);
    }

    static class ExplicitThis {
        int x;
        ExplicitThis(int a) {
            this.x = a;
        }
    }

    static class ImplicitThis {
        int x;
        ImplicitThis(int a) {
            x = a;
        }
    }
}

I've checked the definition of Node within the library LinkedList implementation, and it uses an explicit this - which I expected, but I'm confused why the implicit class compiles at all... does anyone know why / where this is documented?

0 Answers
Related