package object; public class StaticDetail { //2:赋初值~ { System.out.println("匿名代码块"); } //1:只在类加载时,执行一次 static { System.out.println("静态代码块"); } //3 public StaticDetail(){ System.out.println("构造方法"); } public static void main(String[] args) { StaticDetail staticDetail = new StaticDetail(); System.out.println("==============="); StaticDetail staticDetail1 = new StaticDetail(); } }
运行结果:
在代码中直接调用导入的方法名和属性
package object; //静态导入包 import static java.lang.Math.random; import static java.lang.Math.PI; public class StaticImportPackage { public static void main(String[] args) { System.out.println(random()); System.out.println(PI); } }