static
package OOP.Demo06;
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.age);
System.out.println(student.score);
}
}
package OOP.Demo06;
//类的加载顺序:静态代码块>匿名代码块>构造方法
public class Person {
{
System.out.println("匿名代码块");//匿名代码块可以赋初始值
}
static {
System.out.println("静态代码块");//static只执行一次
}
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("=============");
Person person1 = new Person();
}
}
package OOP.Demo06;
//导入静态包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}