jeudi 17 décembre 2020

Java static initialisers when class not explicitly used

I've got a Java class that isn't explicitly referenced anywhere in my application - the intention is that it will be constructed by reflection. Is it expected that the static initialiser won't run if it's not referenced by any other class and no instance is explicitly constructed? That is:

class Test {   
    static { 
        System.out.println("static block"); 
    } 
} 
  
class Main { 
    public static void main(String args[]) { 
        /* The above print only seems to happen if you uncomment the next line */
        // new Test();

        /* Stuff that doesn't involve Test in any way */
        ...
        ...
    } 
}

won't hit that println?





Aucun commentaire:

Enregistrer un commentaire