Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to create Singleton Class in java ?

user-image
Question added by Mohammed Javeed , R&D Engineer , Nokia Solutions and Networks Pvt Ltd
Date Posted: 2017/11/19
amit kumar  pandey
by amit kumar pandey , PHP Developer , technobrix

we can create the Singleton class by using private access modified with its constructor declaration

Ashkan Keshavarzi
by Ashkan Keshavarzi , Senior Java developer , PeykAsa

Singleton class have private constructor and one private refrence to the current objects, also have getInstance function return that private refrence to each point you called it

Singleton class means you can create only one object for the given class. You can create a singleton class by making its constructor as private, so that you can restrict the creation of the object. Provide a static method to get instance of the object, wherein you can handle the object creation inside the class only

IRFAN KHAN
by IRFAN KHAN , Lead Executive , Marlabs Innovation PVT LTD

First need to create private constructot inside a class and will create one static method which should return its on class type, inside  method we will using if condition and will check,if it is new instance then will allow to create instance otherwise will return the last. 

Deenal Shah
by Deenal Shah , Software Engineer , ATMECS TECHNOLOGIES

public class MySingleTon {           private static MySingleTon myObj;     /**      * Create private constructor      */     private MySingleTon(){               }     /**      * Create a static method to get instance.      */     public static MySingleTon getInstance(){         if(myObj == null){             myObj = new MySingleTon();         }         return myObj;     }           public void getSomeThing(){         // do something here         System.out.println("I am here....");     }           public static void main(String a[]){         MySingleTon st = MySingleTon.getInstance();         st.getSomeThing();     } }

Partha Sarathi Ghosh
by Partha Sarathi Ghosh , Developer

public class Singleton { private static Singleton singleton = new Singleton( ); /* A private Constructor prevents any other * class from instantiating. */ private Singleton() { } /* Static 'instance' method */ public static Singleton getInstance( ) { return singleton; } /* Other methods protected by singleton-ness */ protected static void demoMethod( ) { System.out.println("demoMethod for singleton"); } }

Preetam Mukherjee
by Preetam Mukherjee , Digital Interaction Advisor , [24]7.ai

To design a singleton class:

  1. Make constructor as private.
  2. Write a static method that has return type object of this singleton class.

Khalid Habib
by Khalid Habib , Software Engineer , UBS

To design a singleton class:

  1. Make constructor as private.
  2. Write a static method that has return type object of this singleton class. Here, the concept of Lazy initialization in used to write this static method.

Make a constructor as Private

write a static method

inayatullah Shaik
by inayatullah Shaik , Senior Java Developer , Wipro Limited

//Given name to your class, by suffxing Single, it indicates that it is a single ton //class

 

//If we cannot implement, then end user can clone the declared object.

public class MyXXXSingleton implements Cloneable {

 

private static MyObject obj=null;

@Override

protected Object clone() throws CloneNotSupportedException {

throw new CloneNotSupportedException("Singleton Class does not support this method.");

}

 

private MyXXXSingletons(){

//Do any initializaions if necessary

}

public MyXXXSingleton static getInstance(){

if(obj==null){

obj= new MyObject();

returb obj;

}else{

return obj;

}

}

 

}

 

Hasan Mobarak
by Hasan Mobarak , Software Engineer (Java) , Fakir Knitwear's ltd

Code Example of Create own Singleton Class in java:

class Test {

    private static Test t = null;

   // if constructor is public then it is possible to create many object

    private Test() {} 

    public static Test getTest(){

        if(t == null){

            new Test();

        }

        return t;

   }

// Now if we just call Test t1 = Test.getTest() method, we can get benefit of singleton class object, for re-usability, save memory and incise memory performance.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.