Odd situation for "cannot reference this before supertype constructor has been called"

Viewed 9967

Why doesn't this code compile?

public class A {
    public class B extends A {
        public B(A a) { }
    }
    void foo() {
        A a = new A();
        new B(a) { };
    }
}

A.java:[7,17] cannot reference this before supertype constructor has been called

Compilation is successful if either of these changes are made:

  • B is private instead of public
  • line 7 reads new B(A); instead of new B(A) { }

Using javac version: 1.6.0_20

2 Answers
Related