Search code examples
javainstantiation

Is this properly termed "instantiation" if no instance is created?


I have a class that only contains static methods. In order to make use of it, I execute the following code once, early in the project:

new SoundUtility();

Is this line of code considered to be an act of "instantiation" if no actual instance is obtained? All invocations in the project of the utility are static. Or is there another term to use to describe this?

If I don't execute the shown line of code, a static call throws an NPE.


Solution

  • You're calling a constructor and creating an object. Even if you throw the resulting object away immediately and the only effect was to initialize some class level global thing, it still counts as instantiating an object.

    This is a really ugly way to do things, in case that wasn't evident. If you are going to have static state, which is bad enough, it would be better to initialize it in a static initializer than in a constructor call.