How to force a class to be initialised? [closed]

Loading != Initialization.

You want your class to be initialized (this is when static blocks executed, among other things).

An excerpt from the Java Language Specification says:

A class or interface type T will be initialized immediately before the first occurrence of >any one of the following:

  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

Invocation of certain reflective methods in class Class and in package java.lang.reflect also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.

Doh, anovstrup, already said it: Just make an empty static function called init. Be sure to document that well… I personally can’t see any use case for this in the context of well formed code though.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)