static关键字详解
package com.oop.demo07;
public class Student {
private static int age;//静态的变量 多线程!
private double score;//非静态的变量
public void run(){
go();//非静态方法可以调用所有静态方法里的东西
}
public static void go(){//和类一起加载
}
public static void main(String[] args) {
/*Student s1 = new Student();
System.out.println(Student.age);//静态变量可以通过类名调用
//System.out.println(Student.score);//非静态变量不可以通过类名调用
System.out.println(s1.age);
System.out.println(s1.score);*/
new Student().run();//调用非静态方法
Student.go();//调用静态方法
go();//因为它正好在当前这个类里,所以调用静态方法甚至可以不用类名
}
}
package com.oop.demo07;
public class Person {
//2:赋初值
{
System.out.println("匿名代码块");
}
//1:只执行一次~
static {
System.out.println("静态代码块");
}
//3
public Person(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person = new Person();
System.out.println("==============");
Person person2 = new Person();
}
}
package com.oop.demo07;
//静态导入包
import static java.lang.Math.PI;
import static java.lang.Math.random;
public class Test {
public static void main(String[] args) {
System.out.println(random());
System.out.println(PI);
}
}