Static关键字
public class Demo {
//赋初值
{
System.out.println("匿名代码块");
}
//只执行一次
static {
System.out.println("静态代码块");
}
public Demo(){
System.out.println("构造方法");
}
public static void main(String[] args) {
Demo demo = new Demo();
System.out.println("==========");
Demo demo1 = new Demo();
}
}
输出:
静态代码块
匿名代码块
构造方法
匿名代码块
构造方法
//静态导入包
import static java.lang.Math.random;
public class Demo02 {
public static void main(String[] args) {
//System.out.println(Math.random());正常调用
System.out.println(random());//静态导入包可直接调用
}
}