public abstract class ClassLoadingTest { public static void main(String[] args) { User user3 = new User(); } }
public class User { public static User user= new User("wang",18); public static void userSay(){ System.out.println("调用userSay静态方法"); user.say(); } static{ User.userSay(); System.out.println("进入静态构造代码块"); User user2333 = new User("wang2333", 19); user2333.say(); user.say(); } { System.out.println("进入代码块"); } private String name; private int age; public User() {} public User(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void say(){ System.out.println(this.getName() + "调用say方法"); } }
执行结果:
进入代码块
调用userSay静态方法
wang调用say方法
进入静态构造代码块
进入代码块
wang2333调用say方法
wang调用say方法
进入代码块