我的Java程序是这个错误,百思不得其解,求高人指点一二。The method main cannot be declared static; static methods can only be declar第30行

package Example01;
/*

  • 实现类的封装 */ class Student{ private String name ; //将name私有化 private int age ;

//下面公有方法间接访问类中属性值getxxx方法,setxxx方法
public String getName(){
return name;
}
public void setName(String stuName) {
name = stuName;
}
public int getAge() {
return age;
}
public void setAge(int stuAge) {
if(stuAge < 0){
System.out.println("不合法");
}else{
age = stuAge;
}

}
public void introduce() {
System.out.println("大家好,我叫"+name + ",我今年"+ age +"岁");

}
public class example01 {
public static void main(String[] args) {
Student stu = new Student() ;
stu.setAge(30);
stu.setName("韩强");
stu.introduce();
}
}
}

我看到你好像在程序最下面加多了一个 } 号,JAVA编译器可能会因为这样而出现奇怪的解说。

public class example01 { 这个类需要单独定义为一个类文件,或者去掉它直接写 main 方法:

public class Student {
    private String name; // 将name私有化
    private int age;

    // 下面公有方法间接访问类中属性值getxxx方法,setxxx方法
    public String getName() {
        return name;
    }

    public void setName(String stuName) {
        name = stuName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int stuAge) {
        if (stuAge < 0) {
            System.out.println("不合法");
        } else {
            age = stuAge;
        }

    }

    public void introduce() {
        System.out.println("大家好,我叫" + name + ",我今年" + age + "岁");

    }

    public static void main(String[] args) {
        Student stu = new Student();
        stu.setAge(30);
        stu.setName("韩强");
        stu.introduce();
    }
}

The method main cannot be declared static; static methods can only be declared in a static or top level type

内部类想要定义静态方法,那需要这个内部类也是静态的
public static class example01{

}