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

PREVIOUS NEXT
Code Example
Java :: lighting strike solved 
Java :: iterade dict javacirpt 
Java :: java swing place objects vertically 
Java :: Which Is Better to Configure a Spring Boot Project — Properties or YAML? 
Java :: convert kotlin to java online editor 
Java :: choose image java 
Java :: viewResolver 
Java :: display pop up window overlay permission in redmi android studio 
Java :: component spring 
Java :: binary search tree 
Java :: java hashtable 
Java :: remove part of string java 
Java :: Java if...else...if Statement 
Java :: convert long to localdatetime java 
Java :: java enum from int 
Java :: java jcombobox selected item changed 
Java :: Search a 2D Matrix II 
Java :: extract substring in string java 
Java :: java get class by string name 
Java :: devotional meaning 
Java :: code wars jaden casting java 
Java :: in java write a code that suppose the following input is supplied to the program: 9 Then, the output should be: 12096 (99+999+9999+999) 
Sql :: sql server reset identity 
Sql :: Find all triggers in database 
Sql :: mysql find tables with name 
Sql :: mysql workbench in ubuntu 14.04 
Sql :: mysql get date diff in months 
Sql :: start mysql server linux terminal 
Sql :: postgresql truncate cascade restart identity 
Sql :: dbms_output sql developer 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =