Error
No enclosing instance of type <ClassName> is accessible. Must qualify the allocation with an enclosing instance of type <ClassName> (e.g. x.new A() where x is an instance of <ClassName>).
Error Type
Compilation
Cause
Trying to initialize the instance of inner class without specifying the instance of outer class. Inner classes are not accessible directly using the Outer class name as only static inner classes are accessible that way.
Sample Code
public class OuterClass {
public class InnerClass {
}
}
OuterClass.InnerClass = new OuterClass.InnerClass(); // this line gives error
Resolution
Either make the inner class as static inner class
public class OuterClass {
public static class InnerClass {
}
}
OuterClass.InnerClass = new OuterClass.InnerClass();
But with this, the copy of the inner class will not be object specific but class specific
Or
Initialize the Inner class using the Outer class instance
public class OuterClass {
public class InnerClass {
}
}
OuterClass.InnerClass = new OuterClass.new InnerClass();
Either make the inner class as static inner class
public class OuterClass {
public static class InnerClass {
}
}
OuterClass.InnerClass = new OuterClass.InnerClass();
But with this, the copy of the inner class will not be object specific but class specific
Or
Initialize the Inner class using the Outer class instance
public class OuterClass {
public class InnerClass {
}
}
OuterClass.InnerClass = new OuterClass.new InnerClass();