This typically occurs when you "use internal classes in static methods"
Test code:
public class Test {public static void main (string[] args) {A A = new A (1);} Class A {int x;public a () {}public A (int x) {this.x = x;}}}
The above code in the main function will have a compile error, in order to solve the problem, let us first look at the compiler prompt:
The compiler tells us that there is no instance of the test class that is accessible, which means that the instance is going to be used, and that place is used? Looking at the parentheses in the back, "x.new A ()", New A () is actually x.new a (), where X is an instance of test. So we can first create an instance of test and then use the new method of this instance to new A ().
You can then change to the following code:
public class Test {public static void main (string[] args) {test test = new test (); A = Test.new A (1);} Class A {int x;public a () {}public A (int x) {this.x = x;}}}
Understand this to explain the following code:
public class Test {public static void main (string[] args) {test test = new test (); A = Test.new A (1); b b = new B (2);//The b here is not an internal class, so there is no such problem so can be directly written newa.aa AA = a.new AA (3);} Class A {int x;public a () {}public A (int x) {this.x = x;} Class AA {int x; AA () {}aa (int x) {this.x = x;}}}} Class B {int x;public B () {}public B (int x) {this.x = x;}}
Alternatively, change the inner class to static by using an external method:
public class Test {public static void main (string[] args) {A A = new A ();} Static Class A {int x;public a () {}public A (int x) {this.x = x;}}}
Workarounds for "No enclosing instance of type * is accessible" in Java