3

I have a that class does not really specify any real object on its own but is more of a utility class. It has some data whose initialization is expensive. I see two options on how to store the data:

  1. Store it in a static variable(initialized in a static block).
    • Pro: I can call the utility methods without having to create an instance of this class.
    • Con: The heavy initialization code is run when the class is loaded, independent of whether it is used or not.
  2. Store it in an instance variable(initialized in the constructor):
    • Pro: The heavy initialization is only done if the class is used.
    • Con: In this case I will have to create an object of the class and then call the utility methods through this object. I will also need to avoid creating multiple objects of the utility class.

Which of these two approaches is better?

4

If you're storing data of any kind in the class, especially if it's an expensive item of data, you should be storing it as an instance variable.

Here's why: you need to manage the lifetime of your classes that store data. When the object goes out of scope, your memory gets cleaned up by the garbage collector automatically, including the instance variable.

If memory serves, you can still call static utility methods on the class; you'll just have to pass your expensive data into the utility method, just like you would with any static utility method. In general, I would avoid static state unless you have a very good reason for needing it.

| improve this answer | |
  • What about in a situation where a few different portions of my programme need to access this data? If I used an instance variable in this case, then the heavy initialization code would be run multiple times and I could potentially have multiple copies alive simultaneously. – navari Jun 7 '15 at 19:30
  • 1
    @navari: Not necessarily, you can create only one instance of the shared data and pass it as an argument to constructors in all the portions of your program that need it. – Giorgio Sep 2 '15 at 9:39
1

There are other options. You could use a Singleton where you only initialize the static instance if you call getInstance().

MathUtils.java

public class MathUtils {

    private static MathUtils mathUtils;

    private MathUtils() {
        //initialization logic here
    }

    public static MathUtils getInstance() {
        if (mathUtils == null ) {
            mathUtils = new MathUtils();
        }
        return mathUtils;
    }

    public int add(int a, int b) {
        return a + b;
    }
}

Main.java

public class Main {

    public static void main(final String[] args) {
        MathUtils mathUtils = MathUtils.getInstance();
        int addition = mathUtils.add(1, 3);
        System.out.println(addition);
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.