java中当static块和构造函数同时出现,顺序是?

静态块先于构造函数执行

 

class Student {
    int age;
    String name;
    static int count;

    public Student() {
        System.out.println("in constructor");
    }
/*只执行一次。*/
    static {
        System.out.println("hello");
        count = 0;
    }
}

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        Student s1 = new Student();
    }
}

 

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_44639795/article/details/103129412

posted @ 2021-01-12 09:53  师徒行者  阅读(69)  评论(0编辑  收藏  举报