#同类一道加载
package base.oop.Demo09;
//static
public class Student {
private static int age;//静态的变量
private double score;//非静态的变量
public void run(){
}
public static void go(){
}
public static void main(String[] args) {
/* Student student = new Student();
System.out.println(Student.age);
System.out.println(Student.score);
System.out.println(student.score);
System.out.println(student.age);*/
go();//因为go方法是静态的变量,同类一起加载,所以可以直接调用Student.go()或者可以直接不写Student.
//第2部分:Student.run();//因为run不是静态方法需要实例变量出来才可以调用
}
}
#匿名代码块、静态代码块、构造方法的输出顺序
package base.oop.Demo09;
public class Person {
{
System.out.println("匿名代码块");
}
static {
System.out.println("静态代码块");
}
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();//最后的输出结果为静态代码块、匿名代码块、构造方法
System.out.println("========================");
Person person1 = new Person();//输出的结果为匿名代码块、构造方法,说明static只执行一次
}
}