静态、非静态属性和方法 调用的区别
public class Student {
private static int age;
private double score;
public static void run(){
System.out.println("run");
}
public void go(){
System.out.println("go");
run();
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(Student.age);
System.out.println(s1.score);
Student.run();
run();
s1.go();
}
}
静态代码块、匿名代码块、构造函数的执行顺序
public class Person {
{
System.out.println("匿名代码块");
}
static {
System.out.println("静态代码块");
}
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
}
}
静态导入包
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);
}
}