jeudi 5 septembre 2019

Constructor newInstance generates local instance only

Looks I miss something.

I have following class with Singleton:

public class Core{

   private static Core instance = new Core();

   public static Core getInstance() {
        return instance;
    }   

   public static void destroy(){
    instance = null;
   } 
}

In my tests I kill the instance with Core.destroy() and therefore Core.getInstance() returns null.

So every test I want to re-generate instance again. I do the following:

Constructor<Core> constructor = Core.class.getDeclaredConstructor();
                constructor.setAccessible(true);
                Core newCore = constructor.newInstance();

So now newCore is initialized but Core.getInstance() still returns null.

How to initialize properly Core -> instance?





Aucun commentaire:

Enregistrer un commentaire