Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the best and cleanest way to do this? Specifically, I need some code in a static initializer block to run in that class, but I'd like to make this as clean-looking as possible.

like image 693
Artem Avatar asked Aug 24 '10 19:08

Artem


People also ask

How do you initialize a class?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

Can we initialize a class without making constructor?

The only way you can make an object is to construct it - so you can never initialize a class without a constructor.

How do you make a public class not initialized by other classes?

How to make a public class not to be initialised by other classes **Making class as private. *Converting class to an interface. *Creating private constructor for the class. *Converting class to an abstract class.

What is init () method in Java?

Init method is a predefined method to initialize an object after its creation. Init method is a life cycle method for servlets for java. It is started by the browser when java program is loaded and run by the browser. Init method is a predefine method to initialize an object after its creation.


2 Answers

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.


You can use the following code to force initialization of a class:

//... Foo.class ...          //OLD CODE ... forceInit(Foo.class) ... //NEW CODE  /**  * Forces the initialization of the class pertaining to   * the specified <tt>Class</tt> object.   * This method does nothing if the class is already  * initialized prior to invocation.  *  * @param klass the class for which to force initialization  * @return <tt>klass</tt>   */ public static <T> Class<T> forceInit(Class<T> klass) {     try {         Class.forName(klass.getName(), true, klass.getClassLoader());     } catch (ClassNotFoundException e) {         throw new AssertionError(e);  // Can't happen     }     return klass; }  
like image 45
Iulian Avatar answered Sep 28 '22 00:09

Iulian