views:

565

answers:

4

Hi, I've created a class which holds a bunch of properties values. In order to initialize that class, I have to call some static method "configure()" which configures it from an XML file.

That class was supposed to act to store some data such that I could just write

PropClass.GetMyProperty();

I call the configure() from a static block in the main so I can use it anywhere

BUT

If I set a static constant member of some other class to a value from my "PropClass", I get null,

class SomeClass {

   static int myProp = PropClass.GetMyProperty();

}

That's probably because that expression is evaluated before the call to configure. How can I solve this issue?

How can I enforce that the call to configure() will be executed first? Thanks

A: 

Can you not make the GetMyProperty() method check whether configure() has been called already ? That way you can call GetMyProperty() without having to worry about wheher our object is configured. Your object will look after this for you.

e.g.

public String getMyProperty() {
   if (!configured) {
      configure();
   }
   // normal GetMyProperty() behaviour follows
}

(you should synchronise the above if you want to be thread-safe)

Brian Agnew
+5  A: 

you could use a static code block to do that

static {
   configure();
}

the syntax of a static initializer block? All that is left is the keyword static and a pair of matching curly braces containing the code that is to be executed when the class is loaded. taken from here

Peter Perháč
It might be safer to do away with the configure method altogether if you want the code to be executed once only (barring any class loader tricks). It's also worthwhile noting that this approach is threadsafe.
CurtainDog
It really is this simple.
Steve Reed
A: 

Dude, sounds like you should be using Spring Framework (or some other Dependency Injection framework). In Spring, you already get everything that you need:

  • An XML format for defining beans with configurable properties, no need to code the logic for reading the XML and initializing the beans yourself.
  • Beans are initialized when you need them (provided that you access them in the correct manner). The best way would be to inject the beans into the callers.

Don't invent the wheel... Spring is one of the most commonly used frameworks in Java. IMHO, no large Java application should be coded without it.

zvikico
A: 

I would do the following:

class SomeClass 
{
   // assumes myProp is assigned once, otherwise don't make it final
   private final static int myProp;

   static
   { 
       // this is better if you ever need to deal with exceeption handling, 
       // you cannot put try/catch around a field declaration
       myProp = PropClass.GetMyProperty();
   }
}

then in PropClass do the same thing:

class PropClass
{ 
    // again final if the field is assigned only once.
    private static final int prop;

    // this is the code that was inside configure.
    static
    {
        myProp = 42;
    }

    public static int getMyProperty();
}

Also. if possible, don't make everything static - at the very least use a singleton.

TofuBeer
Singleton is not any better. Simply don't use any static mutable data, including singletons. http://www.youtube.com/watch?v=-FRm3VPhseI
Esko Luontola
I don't disagree overall - but singleton is better in that it is easier to remove the dependency on a static instance tan it is to remove all of the static methods/vars.
TofuBeer
also, you will notice that I was preferring final (immutable) instead of non-final (mutable). But even with immutable I'd rather avoid the static as much as possible.
TofuBeer