Items 2: Enforce the Singleton property with a private constructor

A singleton is a simple class that is instantiated only once.

Best method for using a singleton:

public class CustomerList {
   private static final CustomerList INSTANCE = new CustomerList();

   private CustomerList()
   public static CustomerList getInstance()
      return INSTANCE;

   .. // Remainder omitted

Main advantage of this approach is it allows for the flexibility to change your mind about the class without changing the API.
Use this approach when you uncertain to the dynamics of the class.
If you are certain it will forever remain a singleton, stick to a “public static final” as opposed to a private through public accessor.

Don't be shellfish...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrEmail this to someone

Leave a Reply