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 :: how to add a listener to a toggle group radio buttons javafx 
Java :: hashmap in java 
Java :: get index of element in array java 
Java :: java convert pdf to image 
Java :: how to check if a person presses a button in jframe in java 
Java :: get date from calendar java 
Java :: équivalent setTimeInterval java 
Java :: java get class name of object 
Java :: implement two interfaces java 
Java :: debug in java 
Java :: tableau deux dimensions java 
Java :: how to read text file as variable in java 
Java :: what are abstract methods in java 
Java :: how to use string variables with an if statement in java 
Java :: codepointat java 
Java :: java read from file word by word 
Java :: Java Converting double into an int 
Java :: java get element occurrences in a list java 
Java :: combinations in java 
Java :: and roid shape setCornerRadii 
Java :: java Convert a string IPv4 IP address to the equivalent long numeric value. 
Java :: jaccard index two vectors R 
Java :: Error: Could not find or load main class Main Caused by: java.lang.ClassNotFoundException: Main 
Java :: java do while loop example 
Java :: how to create a console in java gui 
Java :: how to create Java ArrayList 
Java :: convert pdf to word in java 
Java :: iterative inorder traversal 
Java :: org.springframework.security.oauth2.jwt.JwtEncoder 
Java :: contient java 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =