0
static boolean isClassLoaded(String fullname) {
    try {
        Class.forName(fullname, false, Loader.instance().getModClassLoader());
        return true;
    } catch (Exception e) {
        return false;
    }
}

does this method has potential to trigger fullname's static initializer ? i have problem with static initializer called twice. when i try to check if class loaded using isClassLoaded and try to use that class, i get error because of constructor called twice. anyone know what is problem with Class.forName(fullname, false, Loader.instance().getModClassLoader()); ?

  • 3
    It’s not clear where `Loader.instance().getModClassLoader()` comes from and what it does. Further, you are talking about a “constructor called twice” but not what constructor of which class and how this relates to the “static intitializer” of your question’s title. – Holger Oct 09 '15 at 14:36
  • I could replace them with ThisClass.class.getClassLoader(); – MCdevelopers Oct 09 '15 at 14:42
  • In that case, no initializer should get invoked and whatever exception in whatever constructor occurs has nothing to do with static initializers. – Holger Oct 09 '15 at 14:46

1 Answers1

1

The second parameter is a flag called "initialize".

From the docs:

The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

So, if initialize is set to false, it will not execute your static initializers.

Self-contained example

package test;

public class Main {

    public static void main(String[] args) throws Exception {
        Class.forName("test.Main$Foo", false, Main.class.getClassLoader());
        System.out.println("blah");
        Class.forName("test.Main$Foo", true, Main.class.getClassLoader());
    }

    static class Foo {
        static {
            System.out.println("Foo static initializer");
        }
    }

}

Output

blah
Foo static initializer

Note it would always print Foo static initializer only once, but here, it prints blah first, i.e. the first Class.forName invocation did not execute the static initializer.

Mena
  • 47,782
  • 11
  • 87
  • 106