answersLogoWhite

0

How is the Singleton Pattern implemented in Java?

Updated: 8/19/2019
User Avatar

Wiki User

13y ago

Best Answer

Singleton pattern is a design pattern wherein there is only one instance of a particular class at any point during the system existence. It is implemented by:

a. Making the constructor private (so that no other class can create an object of this class using the new keyword) and

b. Writing a getInstance() method which will return an instance of the class.

Ex:

Public class SingletonExample {

Private static SingletonExample _instance;

private SingletonExample() {

super();

… //Other initialization code

}

public static SingletonExample getInstance() {

If(_instance == null){

_instance = new SingletonInstance();

}

Return _instance;

}

}

Any class that wants an instance of this class can do as follows:

SingletonExample object = SingletonExample.getInstane();

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How is the Singleton Pattern implemented in Java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Who firstly implemented Java compiler in Java?

James A. Gosling for Sun Microsystems implemented it on a PDA device named Star before it was named Java. As for who implemented it under the Java name first is near impossible to determine.


What are the feature of java which have been implemented in c?

None, as C was made prior to Java.


What java interface must be implemented by all threads?

Runnable interface


What is java singleton object and why you need it?

Singleton pattern is a design pattern wherein there is only one instance of a particular class at any point during the system existence. It is implemented by: a. Making the constructor private (so that no other class can create an object of this class using the new keyword) and b. Writing a getInstance() method which will return an instance of the class. Ex: Public class SingletonExample { Private static SingletonExample _instance; private SingletonExample() { super(); … //Other initialization code } public static SingletonExample getInstance() { If(_instance == null){ _instance = new SingletonInstance(); } Return _instance; } } Any class that wants an instance of this class can do as follows: SingletonExample object = SingletonExample.getInstane();


What year java was implemented?

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform.Newer versions of Java have been released on a regular basis...


What is meant by native method in java?

The Java native method is a great way to gain and merge the power of C or C++ programming into Java. To use Java as a scientific and high performance language, when efficient native Java compilers are not fully implemented, use native method can boost the performance to at least the speed of C compiled code.


How is Java persistence done?

Java persistence is implemented using serialization. Serialization is a technique in java using which the contents of a java object (A class instance) can be written into a flat file. This value can be unserialized or deserialized at a later point of time to create the object. Any class that implements the Serializable interface can be serialized.


Why java is not purely object oriented programming language?

Java IS a pure OOP language. All types, including the built-in types, are implemented as objects.


Are there any Air Java adapters that has already been implemented?

Merapi is a bridge between AIR and Java. It is currently in alpha-testing and they are looking for AIR developers to help beta test their bridge.http://www.merapiproject.net/


What is light weight component in java and why is it so?

Swing is considered light weight because it is fully implemented in Java, without calling the native operating system for drawing the graphical user interface components


Who developed the JAVA software?

James Gosling was the originator of Oak, the programming language that eventually evolved into Java. He retained a large role in overseeing the design of the Java language itself. The primary developer of the JVM and JDK (and related software) which implemented the Java language was Sun Microsystems, Inc. They were bought out by Oracle, Inc. in 2009.


Data Structures and Algorithms in Java?

Data structures has been implemented in Java language already, you just need to import it and start using it. Data Structures are located in Java.util packages.ArrayArraylistVectorHashMapHashTableLinkedListStackQueueCollection this are the few I know.Thanks,Anandkumar.R