samedi 26 mars 2016

Singleton class object referenced by null

I have a question, where i have a singleton class and it's instance is referenced in many places in my project. Now the question is what happens at one the place the instance is assigned with null reference. Will it point to null reference rest of the places, If this is the case how can i avoid it?

public enum Test {
INSTANCE;
    public void fun(){
    System.out.println("hello");
}  

}

public class Main {

public static void main(String[] args) {
    Test test = Test.INSTANCE;
    test.fun();
    test = null;
    test.fun();
}

}

Aucun commentaire:

Enregistrer un commentaire