How to implement or use the singleton class in java?
Answer Posted / 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 |
Post New Answer View All Answers
What are the uses of the set interfaces in the java collections? : java collections
Hi frnds how to lock an user when he enter wrong credentials more than 3 time using java or j2ee tech take username and password in a bean no need to connect DB and validate give me some sample application code or links its urgent for me thanks in advance
What are the different types of ways where you can iterate over a list? : java collections
what is mean by hasing and maping in java platform and advantage?
What are the different types of classes implemented in the set interfaces? : java collections
What are the main classes of the list interfaces? : java collections
please mail me the interview question based on java/j2ee
Explain the OOPS concept in Realtime Scenarion ? Take example as CAR. Please explain indetail ?
Which sorting algorithm is used by collections.sort() in java ?
What are the queues in the java collection framework? : java collections
What are the different types of collections views being provided by the map interface? : java collections
Should we create system software ( e.g operating system ) in java ?
Hi we have an urgent requirement for Java/J2ee technical lead position & also looking for "Java Guidewire claimcentre" experienced professional for Bangalore location if interested can reach srisanh@gmail.com
Hi Friends , am newbie to ajax. For example just consider one account registration - A form contains 8 text fields with submit button. In this form second texbox contains "username " . On right side of text box there is a label box . On clilck action i need to determine user is available or not. Is it possible on clicking label or should i click submit button.
What is iterator in the java collections framework? : java collections