Java 类对象 静态成员变量,静态代码块加载执行顺序。
package com.yjm.pro;
import java.io.IOException;
import java.util.Properties;
public class Pro {
public static String url;
public static String username;
public static String password;
public static String classforname;
public static String log;
public static Properties properties;
static {
System.out.println("类静态代码块 执行test....");
properties = new Properties();
try {
properties.load(Pro.class.getResourceAsStream("pro.properties"));
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
log = properties.getProperty("log");
classforname = properties
.getProperty("classforname");
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试代码
public static void main(String[] args) {
// test1();
// test2();
// test3();
test4();
}
private static void test4() {
System.out.println(Pro.class);
System.out.println("类静态变量" + Pro.log);
}
执行结果
class com.yjm.pro.Pro
类静态代码块 执行test....
类静态变量yes
测试的类 静态数据,在类引用的时候,才会执行静态代码块的数据。
转载于:https://blog.51cto.com/yjm199/1595754