ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

How to create Singleton Class in java ?

user-image
تم إضافة السؤال من قبل Mohammed Javeed , R&D Engineer , Nokia Solutions and Networks Pvt Ltd
تاريخ النشر: 2017/11/19
amit kumar  pandey
من قبل amit kumar pandey , PHP Developer , technobrix

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

Ashkan Keshavarzi
من قبل 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

dhruthi kolla
من قبل dhruthi kolla

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
من قبل 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
من قبل 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
من قبل 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
من قبل 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
من قبل 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.

RaJat Jain
من قبل RaJat Jain

Make a constructor as Private

write a static method

inayatullah Shaik
من قبل 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
من قبل 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.

المزيد من الأسئلة المماثلة