How to instantiate non static inner class within a static method?
今天写了一段这样的代码,然后报错了
1 | public class ceshi2 { |
You have to have a reference to the other outer class as well.
1 | Inner inner = new MyClass().new Inner(); |
If Inner was static then it would be
1 | Inner inner = new MyClass.Inner(); |
What is a static class in Java?
You cannot use the static keyword with a class unless it is an inner class. A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members.
https://www.tutorialspoint.com/What-is-a-static-class-in-Java