Static关键字详解

private static int  age;//静态的变量  多线程!
private double sorce;//非静态的变量
public static void run(){}
public void go(){}
public static void main(String[] args){
Student S1 = new Student();
System.out.println(Student.age);
//System.out.println(Student.sorce);非静态方法
System.out.println(S1.sorce);
System.out.println(S1.age);
run();
new Student().go();

}




{    //作用 : 赋初始值
// 代码块(匿名代码块)
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();

}





//final 之后 无法生成子类
//静态导入包

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);
}
}
posted @ 2020-07-09 09:34  光光1234  阅读(108)  评论(0编辑  收藏  举报