How to instantiate non static inner class within a static method?

今天写了一段这样的代码,然后报错了
1
2
3
4
5
6
7
8
9
10
11
12
public class ceshi2 {

class Student{
private String name;
private Integer age;
}

public static void main(String[] args) {
Student student=new Student();

}
}

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://stackoverflow.com/questions/12690128/how-to-instantiate-non-static-inner-class-within-a-static-method

https://www.tutorialspoint.com/What-is-a-static-class-in-Java