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:
- 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.
- 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?