How to implement or use the singleton class in java?

Answers were Sorted based on User's Feedback



How to implement or use the singleton class in java?..

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

How to implement or use the singleton class in java?..

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

How to implement or use the singleton class in java?..

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

Post New Answer

More Java J2EE AllOther Interview Questions

I have include a jsp page by using <jsp:include page="/.../xyz.jsp"/> The thing is that the xyz.jsp page has its submit button.When i click on that button the whole main page get refreshed.But i want to refresh only the xyz.jsp page.How could i achive it so that i can only refresh the xyz.jsp not the main page page?

4 Answers  


what is mean by hasing and maping in java platform and advantage?

0 Answers   CTS,


What is IOC in spring?

10 Answers   Centris,


What are the different types of collections views being provided by the map interface? : java collections

0 Answers  


what is java virtual machine

4 Answers  






What is the difference between Eclipse and MyEclipse?

7 Answers   IBM, Wavex,


what is acl(access control list)?

1 Answers   Athena, HCL,


What are the different types of ways where you can iterate over a list? : java collections

0 Answers  


Should we create system software ( e.g operating system ) in java ?

0 Answers  


What are the classes in the java collection framework? : java collections

0 Answers  


how to delete cookie information?and when it will delete?

3 Answers   SolutionNET,


Which sorting algorithm is used by collections.sort() in java ?

0 Answers  


Categories