/*

 * MySingletonWKey.java

 *

 * Disclaimer:

 * ZebraSoft.com disclaims all warrantees with regard to documents/programs, including

 * all implied warranties. In no event shall ZebraSoft.com be liable for indirect or

 * consequential damages or any damages whatsoever resulting from loss of use, data or

 * profits arising out of the use of documents/programs.

 *

 * All content included on this site, such as text, graphics, logos, button icons,

 * images, audio clips, video clips, digital downloads, programs, data compilations,

 * and software, is the property of ZebraSoft.com or its content suppliers and

 * protected by United States and international copyright laws.

 *

 * This site or any portion of this site may not be reproduced, duplicated,

 * copied, sold, resold, visited, or otherwise exploited for any commercial

 * purpose without express written consent of ZebraSoft.com.

 */

 

package creational_patterns.my_singleton_w_key;

 

/**

 *

 * @author Sam Eldin

 */

import java.lang.*;

import java.util.*;

import java.io.*;

 

 

public class MySingletonWKey {

   

        private static MySingletonWKey instance;

        public  static int key = -1;

  

    /** Creates a new instance of MySingletonWKey */

    private MySingletonWKey() {

        if(key <= 0)

        {

           System.out.println("Error - Key is not set");

           return;

        }

        key = -1;

    }

    /*

     *

     */

    private synchronized static void createAnInstance(){

        if(null == instance){

            instance = new MySingletonWKey();

            System.out.println("A new MySingletonWKey is created");

        }

    }

    /*

     *

     */

    public  static MySingletonWKey makeInstance(){

        if(key <= 0)

        {

           System.out.println("Error - Key is not set");

            return (null);

        }

        if(null == instance)

            createAnInstance();

        else

           System.out.println("A copy of MySingletonWKey is already created");

        key = -1;

        return instance;

    }

    /*

     *

     */

    protected void finalize()  {

                instance = null;

        key = -1;

        try {

            super.finalize();

                }

                catch(Throwable MyThrowable) {

            System.out.println("finalize() catch clause");

                }

    } 

}



Back to Command