Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java singleton implementation

public class SingletonClass {
    private static final SingletonClass SINGLE_INSTANCE = new SingletonClass();
    private SingletonClass() {}
  public static SingletonClass getInstance() {
      return SINGLE_INSTANCE;
    }
}
Comment

java singleton

- A private constructor
- A static field containing its only instance
- A static factory method for obtaining the instance

public final class ClassSingleton 
{

    private static ClassSingleton INSTANCE;
    private String info = "Initial info class";
    
    private ClassSingleton() 
	{        
    }
    
    public static ClassSingleton getInstance() 
	{
        if(INSTANCE == null) 
        {
            INSTANCE = new ClassSingleton();
        }
  
        return INSTANCE;
    }

    // getters and setters
}

                      =============Usage============
                        

To use our ClassSingleton, we simply need to get the instance statically:

ClassSingleton classSingleton1 = ClassSingleton.getInstance();

System.out.println(classSingleton1.getInfo()); //Initial class info

ClassSingleton classSingleton2 = ClassSingleton.getInstance();
classSingleton2.setInfo("New class info");

System.out.println(classSingleton1.getInfo()); //New class info
System.out.println(classSingleton2.getInfo()); //New class info
Comment

java singleton

public class SingletonOnDemand {
	
	private SingletonOnDemand () {}
	private static class Singleton {
		private static final SingletonOnDemand instance = new SingletonOnDemand();
	}
	
	public static SingletonOnDemand getInstance () {
		System.out.println("create instance");
		return Singleton.instance;
	}
}
Comment

java singleton

public class DB {
    // champs de l'objet

    private DB(){
        // initialisation des champs
    }

    private static final DB INSTANCE = new DB();
    public static DB getInstance(){
        return INSTANCE;
    }
}
Comment

java singleton

public final class SomeSingleton {
   public static final SomeSingleton INSTANCE;

   private SomeSingleton() {
      INSTANCE = (SomeSingleton)this;
      System.out.println("init complete");
   }

   static {
      new SomeSingleton();
   }
}
Comment

Java Singleton Class Syntax

class SingletonExample {

   // private field that refers to the object
   private static SingletonExample singleObject;
                                              
   private SingletonExample() {
      // constructor of the SingletonExample class
   }

   public static SingletonExample getInstance() {
      // write code that allows us to create only one object
      // access the object as per our need
   }
}
Comment

Java Singleton Class Syntax

class SingletonExample {

   // private field that refers to the object
   private static SingletonExample singleObject;
                                              
   private SingletonExample() {
      // constructor of the SingletonExample class
   }

   public static SingletonExample getInstance() {
      // write code that allows us to create only one object
      // access the object as per our need
   }
}
Comment

PREVIOUS NEXT
Code Example
Java :: comments java 
Java :: Implementing the LinkedList Class in java list 
Java :: Passing data from an activity to a fragment 
Java :: convert json to xml in java 
Java :: iterate through arraylist java 
Java :: Java Create a BufferedReader 
Java :: java to kotlin 
Java :: java append to array 
Java :: run specific test case junit 
Java :: loop and save letters in a string java 
Java :: c++ vs java 
Java :: add items to linked list java 
Java :: Java forEach() Method 
Java :: java interview questions for freshers 
Java :: java "-" 
Java :: how to sort linked list in java 
Java :: Print the string after the specified character java 
Java :: l datetime anne month jour heure minute second in java 
Java :: madrid 
Java :: get method jdbc 
Java :: javax.net.ssl.trustStore trustall 
Java :: programme javascrip mineur et majaur 
Java :: gson to class 
Java :: android java navigation bar show icon with title 
Java :: error: package android.support.v4.content does not exist import android.support.v4.content.LocalBroadcastManager; 
Java :: cgange background from button click java fx 
Java :: how to cut a certion part from a string in java 
Java :: acceder a elementos de list java 
Java :: Java Using allOf(Size) 
Java :: how to find a specific character in a string using array 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =