package java08; /* 静态代码块格式: public class 类名称{ static{ //静态代码块 } } 特点:当第一次执行本类时,静态代码块执行唯一的一次 * */ public class Person { static { System.out.println("静态代码块执行"); } public Person(){ System.out.println("构造方法执行!"); } }
package java08; /* 静态代码块格式: public class 类名称{ static{ //静态代码块 } } 特点:当第一次执行本类时,静态代码块执行唯一的一次 静态内容总是优先与非静态,所以静态代码块比构造方法先执行 静态代码块的典型特点: 用来一次性对静态成员变量进行赋值 * */ public class DemoStatic { public static void main(String[] args) { Person one = new Person(); Person two = new Person(); } }