Search code examples
javaclassstaticloader

Is there a way to load a static class without an object?


I have a static class with a static map that I need loaded. I'll get an error and when I debug it, I get SomeClass.someMap class is not loaded." I get that I could create an object of the class, but that kind of defeats the purpose of having it static, doesn't it?

I barely read about ClassLoader, is this something I should use? Or maybe create an own "class loader" which will instantiate the class and other static classes at launch?


Solution

  • If you want to force the class's static initializer to run without creating an instance, you need to initialize the class with Class.forName("somepackage.SomeClass"); -- see the Javadoc.