I have code like the following:
class A {
  final Object data;
  A(Object _data) {
      data = _data;
  }
  class B extends A {
      B() {
          super(new C());
      }
      class C { }
  }
}
I get following error message:
Cannot reference 'C' before supertype constructor has been called
I don't understand why it is not possible.
Are there any workarounds? I want data to be final and classes to be nested as they are in the code above (I don't want to create a different file for each class, because classes are quite small and in my real code it would be more logical for them to be nested)
 
  
                     
                        
With a simple nested class, it would be fine. However, you're creating an inner class of B, which means you're implicitly passing a reference to an instance of B - which would be this in this case, and you can't use this before super(...) completes.
Here's another example - read the comments:
class Base {
   Base(Object x) {}
}
class Outer extends Base {
   Outer() {
       super(new Nested()); // No problem
   }
   Outer(int ignored) {
       super(new Inner()); // Effectively this.new Inner()
   }
   Outer(boolean ignored) {
       super(new Outer().new Inner()); // Fine
   }
   Outer(long ignored) {
       super(new Nested(this)); // Explicitly passing this
   }
   static class Nested {
       Nested() {}
       Nested(Object ignored) {}
   }
   class Inner {
   }
}
 
                    See more on this question at Stackoverflow