类初始化时,先初始化成员变量,然后再执行构造方法。如果成员变量也初始化了一个对象,那么也是先执行成员变量的构造方法,然后再执行该类的构造方法。
测试代码如下:
package com.test.classinit;
public class SystemPropertyProvider {
private String conf_path;
private String root_path;
public SystemPropertyProvider() {
conf_path = "/eas/conf";
root_path = "/opt/msol";
System.out.println("root_path: " + root_path);
}
public String getConfPath() {
return conf_path;
}
private ServerProperties properties = new ServerProperties(getConfPath(), "bbb");
public static void main(String[] args) {
SystemPropertyProvider spp = new SystemPropertyProvider();
}
}
class ServerProperties {
public ServerProperties(String str, String str2) {
System.out.println("str: " + str + ", str2: " + str2);
}
}
str: null, str2: bbb
root_path: /opt/msol