How to implement or use the singleton class in java?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the main classes of the list interfaces? : java collections

785


Which server-side script takes the input from JavaScript, can access the database if it needs to, and processes the data.

1823


What do you understand by synchronization? Why is it important?

789


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.

1801


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

749


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

792


What are the types of interface used in the java collections? : java collections

792


What are the differences between the java collection and the java list? : java collections

809


What is Generic in java? Where can we write Generic ( class or method or objects or etc...)? with simple example? Thanks, Bose. Infosys 2 In which way does a Primitive data type is passed ? Sun- Microsystems 4 what is the use of declaring constructor as private? Sai- Softech 5 what is difference between global methods and local methods? 1 What is meant by class loader and how many types are there? Apple 1 what is meaning of JIT? 4 What is an abstract class? Wipro 5 what is filters and its use? Virtusa 2 why is multiple inheritance not allowed in java? Elementus-Technologies 7 Differences between jdk 1.4 and 1.5 Wipro 4 is JVM platform dependent or independent..? IBM 6 what is main purpose of abstract class? 5 what is difference between colection and collections? Tech-Mahindra 11 What error occurs if a try-catch-finally statement sequence does not have a catch clause? 2 Explain the difference between scrollbar and scrollpane? 1 Explain the differences between public, private, protected and static? 4 Explain the difference between getAppletInfo and getParameterInfo? 1 Is Cegonsoft Pvt.Ltd. a good Institute? 4 Why only one Class is public in one file? Explain in details. Thanks in Advance. 11 Which method will get invoked first in a stand alone application?

2561


can u draw class/object diagram for ATM

5764


What are the interfaces in java collections? : java collections

851


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

2744


who will give req's to u?. how they send req's to u? . what design documents contains?. when bugs raised on other developer code how to report to them?.(throgh mail or ........). how to retrive 100 recods from dao layer to presentation layer.using which collection?. what is sequence diagram.?.

2047


What is the enumerator of the java collection framework? : java collections

768


What are the uses of the set interfaces in the java collections? : java collections

753