无法直接赋值,原因待查找。
可以讲方法获取值放到类的代码块或者静态代码块中。
如下所示:
public class StaticTest {
public static String packageName = "org.yz.抽象工厂模式.抽象工厂模式02";
public static String db;
static Properties properties;
static {
properties = PropertiesUtil.getProperties("src/main/resources/variable.properties");
System.out.println(properties);
db = properties.getProperty("db");
System.out.println(properties.getProperty("db"));
}
public static void changeDB() {
System.out.println(db);
}
}
该篇博客讨论了在Java中如何在类的静态代码块中进行属性初始化,特别是从外部配置文件加载属性值的过程。通过示例代码展示了如何使用Properties类读取资源文件并获取特定属性,以及如何在后续方法中使用这些配置值。

2802

被折叠的 条评论
为什么被折叠?



