Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Can anyone say what is singleton class in java with an sample code

  Asked By: David    Date: Nov 29    Category: Java    Views: 1006
  

Can anyone pls clarify me what is a singleton class along with
sample.Please explain me along with a sample code.

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Debbie Reyes     Answered On: Nov 29

The singleton  is use to asegurate that only one instance of a class  is
instantiated
Why ? Because is esential not have two or because there arent reason
for have two for example Factories, Loggers , ....

Example:
****************************************************
public class Registry {
//----------------------------------------------------------------------------
private Registry(){}
//----------------------------------------------------------------------------
private static Registry registry;
public static Registry getInstance(){
if (Registry.registry==null) Registry.registry = new Registry();
return Registry.registry;
}

public String doSomething(){.....}
// methods publics....
}

****************************************************
public class UseSingleton{
public UseSingleton(){
Registry.getInstance().doSomething();
}

}

 
Answer #2    Answered By: Leroy Schmidt     Answered On: Nov 29

singleton  is not so much a class  as a design pattern to build something
that meets a given intent.

In the example of the Singleton design pattern, the intent is to ensure a
class only has one instance and provides a global point of access to it. It
ensures that all objects that use an instance of this class are using the
same instance.



Example



package singleton;
import javax.swing.*;

public class SingletonFrame extends JFrame {
private static SingletonFrame myInstance;

// the constructor
private SingletonFrame() {
this.setSize(400, 100);

this.setTitle("Singleton Frame. Timestamp:" +
System.currentTimeMillis());

this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
}

public static SingletonFrame getInstance() {
if (myInstance == null)
myInstance = new SingletonFrame();

return myInstance;
}

}

First of all, notice that the class only has one constructor and its access
modifier is private. Secondly, to get an instance of that class, you have
the static getInstance method, and there is also a static variable called
myInstance (of type SingletonFrame). The getInstance method returns the
myInstance variable. The method checks if myInstance is null and, if so,
calls the constructor.

 
Answer #3    Answered By: Edwin Chavez     Answered On: Nov 29

One more example of Using Singleton can be as below.

Say in a web based application you need to load the data which is
required very often.
You can load the data at the first call to the method which loads
the data. Later on whenever the data is required return the same
instance.

In suchj case it will be checked whether the instance already exists.
E.g.

if (properties != null)
{
Invoke the method which returns the properties object.
}
else
{
Return the same object.
}

Just one more example where you can use the Singleton implmentation.

 




Tagged: