0

Suppose I have a class that is loaded using a custom class loader

Class<?> clz = myClassLoader.loadClass("classLoaders.Test");

How can I initialize the class (run its static initializers) without creating an instance, knowing its members, or using reflection?

Gonen I
  • 5,576
  • 1
  • 29
  • 60

1 Answers1

5

To make sure a class is initialized, call Class.forName(String name, boolean initialize, ClassLoader loader) with a true value for the initialize parameter.

Class<?> clz = Class.forName("classLoaders.Test", true, myClassLoader);
Andreas
  • 154,647
  • 11
  • 152
  • 247