static

static


非静态方法可以调用静态方法里面的所有东西,但静态方法只能调用静态方法里面的,不能调用非静态方法里面的;

private static int age;
private double score;
public void run(){

}
public static void go(){

}

public static void main(String[] args) {
    Student s1 = new Student();
    System.out.println(Student.age);
    System.out.println(s1.age);
    System.out.println(s1.score);
    //无法直接使用Student.score,因为score不是一个静态变量
    //无法直接调用run方法,需要new Student().run();
    go();//可以直接调用静态方法
}

静态代码的执行规则:

静态代码块只会执行一次:

posted @ 2023-02-02 12:17  初来乍到的小蜗  阅读(67)  评论(0)    收藏  举报