0

Probably it is discussed somewhere but I failed to find it.

I need to load class properties (java.util.Properties) inside class static initialization block. This is to make possible to access some class general options even without its objects creation. To do so I need appropriate Class object. But of course access to such Class object fails on null object. Something like this.

Class Name {

    private static Properties properties;

    static {
        Name.properties = new Properties();
        Name.properties.load(Name.class.getResourceAsStream("Name.properties"));
    }

}

Any idea how to handle this situation?

UPDATE:
It was resource name (should be "/Name.properties" for my case). Everything else was OK. +1 for all meaningful answers from me and ... don't forget to check operations one by one :-).

Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110

3 Answers3

3

properties field must be static. And before load you need to initialize static variable with proeprties = new Properties() after that you can invoke load

Areo
  • 928
  • 5
  • 12
1

Declare properties as static and initialize

static Properties properties;

or

static Properties properties = new Properties();

and static block should be

static {
    try {
        properties = new Properties(); //if you have not initialize it already
        Name.properties.load(Name.class.getResourceAsStream("Name.properties"));
    } catch (IOException e) {
        throw new ExceptionInInitializerError(e); //or some message in constructor
    }
}

You need to catch IOException while loading properties file

Abdullah Shaikh
  • 2,567
  • 6
  • 30
  • 43
  • 2
    `printStackTrace` is one of the worst ways to handle the exception, unless Roman actually doesn't care if the properties file was loaded. The correct course of action is `throw new RuntimeException(e);`. – VGR Nov 06 '13 at 11:43
  • I know its just that I wanted to be specific to question, the OP needs to handle exceptions properly. I won't be writing entire code for him :) – Abdullah Shaikh Nov 06 '13 at 11:45
  • 1
    An `ExceptionInInitializerError` would be even better IMHO: "Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable." – Gyro Gearless Nov 06 '13 at 11:48
  • @GyroGearless yes ExceptionInInitializerError would be more appropriate – Abdullah Shaikh Nov 06 '13 at 12:04
0

Final code based on all suggestions is like this:

Class Name {

    private static final Properties properties = new Properties();

    static {
        try {
            InputStream stream = Name.class.getResourceAsStream("/Name.properties");
            if (stream == null) {
                throw new ExceptionInInitializerError("Failed to open properties stream.");
            }
            Name.properties.load(stream);
        } catch (IOException e) {
            throw new ExceptionInInitializerError("Failed to load properties.");
        }
    }
}
Roman Nikitchenko
  • 12,800
  • 7
  • 74
  • 110