static关键字详解
static作为静态代码块
package oop.demo01.demo07;
import javax.swing.plaf.synth.SynthOptionPaneUI;
//static
public class Student {
private static int age;//静态变量 可调用静态方法里的。不可调用非静态的
private double score;//非静态变量 可调用静态方法里的
public void run(){
go();
}
public static void go(){
}
public static void main(String[] args) {
go();
}
}
package oop.demo01.demo07;
public class Person {
{//2:赋初始值
System.out.println("匿名代码块");
}
//1:只执行一次
static {
System.out.println("静态代码块");
}
public Person(){//3
System.out.println("构造方法");
}
public static void main(String[] args){
Person person1= new Person();
System.out.println("=======================");
Person person2= new Person();
}
}
静态导入包
package oop.demo01.demo07;
//静态导入包
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);
}
}
标签:Person,System,关键字,详解,static,println,public,out
From: https://www.cnblogs.com/sx-xiaoL/p/17417277.html