How to implement or use the singleton class in java?
Answers were Sorted based on User's Feedback
Answer / amit sharma
singleton is a design pattern. in this per application only
one object should exist.we implement it as follows:
public class abcd
{
private static abcd instance=null;
protected abcd()
{
//only to defeat instantiation;
}
public static abcd getInstance()
{
if(instance==null)
{
instance=new abcd();
}
return instance;
}
| Is This Answer Correct ? | 11 Yes | 4 No |
Answer / aashish r. wadhokar
Singleton is a design pattern as well as class.
when we define singleton class it means we can have only one
object of that class.
We cant create multiple objects of that class.
In singleton class, constructor is always private.
eg:
public class Singleton
{
private static Singleton singletonObject = null;
private Singleton()
{
System.out.println("Singleton object created!!!");
}
public static Singleton CreateInstance()
{
if(singletonObject == null)
{
singletonObject = new Singleton();
}
return singletonObject;
}
Now we can get object as:
Singleton ref = null;
ref = Singleton.CreateInstance();
| Is This Answer Correct ? | 6 Yes | 1 No |
Answer / pinaki mukherjee
A singleton is an object that cannot be instantiated. At
first, that might seem counterintuitive - after all, we need
an instance of an object before we can use it. Well yes a
singleton can be created, but it can't be instantiated by
developers - meaning that the singleton class has control
over how it is created. The restriction on the singleton is
that there can be only one instance of a singleton created
by the Java Virtual Machine (JVM) - by prevent direct
instantiation we can ensure that developers don't create a
second copy.
We'll start with the class definition, for a SingletonObject
class. Next, we provide a default constructor that is marked
as private. No actual code needs to be written, but you're
free to add some initialization code if you'd like.
public class SingletonObject
{
private SingletonObject()
{
// no code req'd
}
}
So far so good. But unless we add some further code,
there'll be absolutely no way to use the class. We want to
prevent direct instantiation, but we still need to allow a
way to get a reference to an instance of the singleton object.
| Is This Answer Correct ? | 3 Yes | 2 No |
What is an algorithm in java collection framework? : java collections
What do you understand by synchronization? Why is it important?
What are the main classes of the list interfaces? : java collections
What are the differences between the java collection and the java list? : java collections
Am chittibabu from vadavalli, My father name is nanjan.he is working in state bank of india at vadavalli, My mother name is ramal. She is house wife. My brother name is rajaram he is working in Privete Company. I was born on 25-january-1984 at bhavanisager in Erode District. I have completed my school in government higher secondary school bhavanisager. After that I came to coimbatore for higher study.i have joined Bsc computer science in sri ramalinga sowdambigai college of science and commerce, affiliated by bharathiyar university, coimbatore. In the same college, I have continued UG degree also. but I got a job in Systech infovation as a software programmer,so I discontinued Msc Degree.I have one and half years experience from this concern. Here I have completed two products; one is Tranport management solution for balaji goods carriers at tirupur. Another one is Embroidery Management Solution for Angel Embroidery at tirupur. After that I got an another job in metis technologies as senior software engineer. I have one year experience from this company.here I working in school management product.it is developing version by version.
what is acl(access control list)?
What are the different types of collections views being provided by the map interface? : java collections
HOW MUCH PERCENTAGE WE DEDUCT FOR OUR SUPPLIERS OR OTHER PROFESSIONALS
VSS Objectives
What is an Ioc pattern? There are assemblies in .net for establishing this task, Whats the special in Java /j2ee technologies about IOC
What is the use of the list interface in the java collection? : java collections
What are the uses of the set interfaces in the java collections? : java collections