For the main method, although written in a class, it is free from any class, so the non-static inner class of a class is not directly visible to it and cannot be accessed directly.
1: Non-static inner class, you must have a reference to an external class to create.
2: In a non-static method of an external class, a non-static inner class can be created directly because there is an implied external class that references this.
3: In the static method of the outer class, because there is no this, you must first obtain the external class reference and then create a non-static inner class.
4: Static inner class that can be created directly without the need for an external class reference.
5: Static internal classes are also not directly accessible to non-static methods of external classes.
6: It can be speculated that the non-static inner class can directly access the method of the external class, because when creating a non-static inner class, there is an implicit external class reference is passed in.
Public classOuterclass { Public Static voidMain (string[] args) {Outerclass Outerclass=NewOuterclass (); Innerclass Innerclass= Outerclass.NewInnerclass (); Staticinnerclass Staticinnerclass=NewStaticinnerclass (); } Public voidfoo () {innerclass innerclass=NewInnerclass (); Staticinnerclass Staticinnerclass=NewStaticinnerclass (); } Public classInnerclass {} Public Static classstaticinnerclass {}}
Why can't an inner class object be created in main () in Java?