简体   繁体   中英

Differences between two singleton examples

Class A 1

public class A {

    private static final A instance = new A();

    public static A getInstance() {
        return new A();
    }

}

Class A 2

public class A {

    private static final A instance = new A();
    private A(){}
    public static A getInstance() {
        return instance;
    }

}

I just start to learn singleton and I saw two java examples that using class A 1 example and class A 2 example. Is the class A 1 getInstance() is singleton? I also like to know what is the differences between these two class A getInstance() method? Thank you

In A1, A is not singleton .. getInstance() is returning a new instance of A everytime

In A2, A is not singleton again, cause the default constructor is still public (implicitly) . One can easily create more instances from outside

EDIT:

Since you have edited the class A in A2, now it becomes singleton.

Here A is created eagerly and will be thread-safe by default. Check lazy vs eager intialization

I also like to know what is the differences between these two class A getInstance() method

Class A 1:

If you look at the code :

 public static A getInstance() {
    return new A();
}

You are returning a new instance of A on each call of getInstance() method and hence it is not a Singleton. Also you have not made the default constructor private in this case and any code outside your class can easily create instances of the class breaking the Singleton paradigm.

Class A 2:

Looking at this code :

public class A {

  private static final A instance = new A();
  private A(){}
  public static A getInstance() {
     return instance;
  }
}

You are returning the same instance for each call of getInstance() .Now your class behaves like Singleton , You are actually doing an eager instantiation of the Singleton instance here and this Singleton instance should be thread-safe. Also make the class final so that no one can sub class it and break the Singleton.

In first case

each time a new instance of A is creating .

* in second case *

As per single ton pattern ,it should be

public class A {
   private static A instance = null;
   private  A() {

   }
   public static A getInstance() {
      if(instance == null) {
         instance = new A();
      }
      return instance;
   }
}

The class A maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.

Is the class A 1 getInstance() is singleton?

No because everytime you are calling this method, a new instance of A is created.

I also like to know what is the differences between these two class A getInstance() method?

The first getInstance() will always create a new instance of class A, the second getInstance() will always return the same instance created of class A.

        There are two ways of creating a singleton class
        1, Dynamic Singleton
        2, Static Singleton

        Static Singleton :

        public class A {
          //Create a object of class A and make it final so that nobody can modify it
          private static final A instance = new A();

          //make the constructor private so that new objects can not be created
          private A(){}

          //provide a method to access the object
          public static A getInstance() {
             return instance;
          }
        }

        Dynamic Singleton :

a, Single Check
        public class A {
    //Create a object reference of class A
    private static A instance = null;

    //make the constructor private so that new objects can not be created
    private  A() {}


    public static A getInstance() {
        //check if instance is null or not
        if(instance == null) {
            if(instance == null) {
                //if null then create an instance of class A and assign it to the final reference
                instance = new A();
            }

        }
        return instance;
    }
}

b, double check
public class A {
    //Create a object reference of class A
    private static A instance = null;

    //make the constructor private so that new objects can not be created
    private  A() {}


    public static A getInstance() {
        //check if instance is null or not
        if(instance == null) {
            synchronized(A.class){
                //Double Check
                if(instance == null) {
                    //if null then create an instance of class A and assign it to the final reference
                    instance = new A();
                }
            }
        }
        return instance;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM