maroph

Java Singleton Sample

Sep 30th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.91 KB | None | 0 0
  1. package com.pastebin.maroph;
  2.  
  3. /**
  4.  * Java Singleton sample.
  5.  * <p>
  6.  * This code is based on the following article:
  7.  * <pre>
  8.  * Title  : Effective Ways to Implement and Use the Singleton Design Pattern
  9.  * Sample : Class Holder Singleton
  10.  * URL    : https://community.oracle.com/docs/DOC-918906
  11.  * </pre>
  12.  * <p>
  13.  */
  14. public class JavaSingleton {
  15.     /** Private constructor */
  16.     private JavaSingleton() {
  17.        /* the body of the constructor here */
  18.     }
  19.  
  20.     /**
  21.      * Access point to the unique instance of the singleton.
  22.      * @return return the singleton
  23.      */
  24.     public static JavaSingleton getInstance() {
  25.         return JavaSingletonHolder.INSTANCE;
  26.     }
  27.  
  28.     /**
  29.      * Private singleton holder class.
  30.      */
  31.     private static class JavaSingletonHolder{
  32.         /** create the singleton instance */
  33.         private static final JavaSingleton INSTANCE = new JavaSingleton();
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment