Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke static initializer again

Once a class is loaded is there a way to invoke static initializers again?

public class Foo {

    static {
        System.out.println("bar");
    }

}

Edit:

I need to invoke the static initializer because I didn't write the original class and the logic I need to invoke is implemented in the static initializer.

like image 292
Kalecser Avatar asked Mar 25 '09 18:03

Kalecser


People also ask

How many times is the static initializer block executed?

2. Static Block. Static initializer block or static initialization block, or static clause are some other names for the static block. Static block code executes only once during the class loading.

How many static initializers can you have?

There can be multiple static initialization blocks in a class that is called in the order they appear in the program.

What is static initializer?

The static initializer is actually invoked later, when the class is initialized, after it has been loaded and linked. That happens when you instantiate an object of a class or access a static variable or method on the class.

What is a static initializer block?

Class static initialization blocks are a special feature of a class that enable more flexible initialization of static properties than can be achieved using per-field initialization.


1 Answers

Put the initalisation code in a separate public static method, so you can call it from the static initializer and from elsewhere?

like image 152
Daniel Earwicker Avatar answered Oct 06 '22 05:10

Daniel Earwicker