目录
代码块、静态导入包、final修饰的类
1.代码块
package test.Demo04;
public class Person {
{
System.out.println("匿名代码块");
}
//静态代码块只执行一次~
static {
System.out.println("静态代码块");
}
public Person() {
System.out.println("构造方法");
}
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("==============");
Person person2 = new Person();
}
}
2.静态导入包
package test.Demo04;
//静态导入包
import static java.lang.Math.random;
import static java.lang.Math.PI;
public class Test {
public static void main(String[] args) {
//不需要写Math.random(),直接random()即可调用
System.out.println(random());
System.out.println(PI);
}
}
final
-
通过final修饰的类不能被继承,也就是没有子类,相当于final之后断子绝孙。