在Java中初始化一个复杂静态成员的最佳方法是什么?(What is the best way to initialize a complex static member in Java?)

编程入门 行业动态 更新时间:2024-10-28 21:26:20
在Java中初始化一个复杂静态成员的最佳方法是什么?(What is the best way to initialize a complex static member in Java?)

我的目标是在我的类中创建一个私有静态Properties对象,以在创建我的应用程序所需的其他Properties对象时充当默认值。 目前的实现如下所示:

public class MyClass { private static Properties DEFAULT_PROPERTIES = new Properties(); static { try { DEFAULT_PROPERTIES.load( MyClass.class.getResourceAsStream("myclass.properties")); } catch (IOException e) { throw new RuntimeException(e); } } }

看着它,它有效,但它感觉不对。

你会怎么做?

My objective is to have a private static Properties object in my class, to act as defaults when creating other Properties objects needed by my application. The current implementation looks like this:

public class MyClass { private static Properties DEFAULT_PROPERTIES = new Properties(); static { try { DEFAULT_PROPERTIES.load( MyClass.class.getResourceAsStream("myclass.properties")); } catch (IOException e) { throw new RuntimeException(e); } } }

Looking at it, it works, but it doesn't feel right.

How would you do it?

最满意答案

基本上有两种方法。 第一种方法是使用静态块(如前所示)(但是使用ExceptionInInitializerError而不是RuntimeException )。 第二种方法是使用你在声明中立即调用的静态方法:

private static Properties DEFAULT_PROPERTIES = getDefaultProperties(); private static Properties getDefaultProperties() { Properties properties = new Properties(); try { properties.load(MyClass.class.getResourceAsStream("myclass.properties")); } catch (IOException e) { throw new ConfigurationException("Cannot load properties file", e); } return properties; }

ConfigurationException可以只是您的自定义类扩展RuntimeException 。

我个人更喜欢static块,因为它没有任何意义,它只能在其生命中执行过一次 。 但是,如果您重构该方法以使其具有文件名并且可以在全局范围内重用,那么这将更受欢迎。

private static Properties DEFAULT_PROPERTIES = SomeUtil.getProperties("myclass.properties"); // Put this in a SomeUtil class. public static Properties getProperties(String filename) { Properties properties = new Properties(); try { properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)); } catch (IOException e) { throw new ConfigurationException("Cannot load " + filename, e); } return properties; }

There are basically two ways. First way is using the static block as you have shown (but then with an ExceptionInInitializerError instead of the RuntimeException). Second way is using a static method which you call immediately on declaration:

private static Properties DEFAULT_PROPERTIES = getDefaultProperties(); private static Properties getDefaultProperties() { Properties properties = new Properties(); try { properties.load(MyClass.class.getResourceAsStream("myclass.properties")); } catch (IOException e) { throw new ConfigurationException("Cannot load properties file", e); } return properties; }

The ConfigurationException can just be your custom class extending RuntimeException.

I personally prefer the static block because it doesn't make sense having a method which is executed only once ever in its life. But if you refactor the method so that it takes a filename and can be reused globally, then that would be more preferred.

private static Properties DEFAULT_PROPERTIES = SomeUtil.getProperties("myclass.properties"); // Put this in a SomeUtil class. public static Properties getProperties(String filename) { Properties properties = new Properties(); try { properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(filename)); } catch (IOException e) { throw new ConfigurationException("Cannot load " + filename, e); } return properties; }

更多推荐

本文发布于:2023-07-30 13:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   静态   成员   方法   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!