publicclassStudent{privatestaticint age;//静态变量privatedouble score;//非静态变量publicvoidrun(){go();}publicstaticvoidgo(){}publicstaticvoidmain(String[] args){
Student s =newStudent();
System.out.println(s.age);
System.out.println(s.score);
System.out.println(Student.age);
System.out.println(Student.score);//Non-static field 'score' cannot be referenced from a static contextrun();//直接调用会编译报错 Non-static method 'run()' cannot be referenced from a static contextgo();
Student.go();}}
2. 静态代码块
publicclassPerson{//Second 可以初始赋值{//匿名代码块
System.out.println("匿名代码块");}//First 只执行一次static{//静态代码块
System.out.println("静态代码块");}//ThirdpublicPerson(){
System.out.println("构造方法");}publicstaticvoidmain(String[] args){
Person per1 =newPerson();
Person per2 =newPerson();}}
OOP --> static关键字1. static变量 static方法public class Student { private static int age; //静态变量 private double score; //非静态变量 public void run(){ go(); } public static void go(){ } public static voi