- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is a singleton class in Java?
A singleton class in Java is the one which can have only one object.
The easiest implementation consists of a private constructor and a field to hold its result, and a static accessor method with a name like getInstance().
The private field can be assigned from within a static initializer block or, more simply, using an initializer. The getInstance( ) method (which must be public) then simply returns this instance −
Example
public class Singleton { private static Singleton singleton = new Singleton(); private Singleton() { } public static Singleton getInstance() { return singleton; } protected static void demoMethod() { System.out.println("demoMethod for singleton"); } }
Here is the main program file where we will create a singleton object −
public class SingletonDemo { public static void main(String[] args) { Singleton tmp = Singleton.getInstance(); tmp.demoMethod(); } }
- Related Articles
- How to make a class singleton in Java?\n
- Singleton Class in C#
- How to prevent Cloning to break a Singleton Class Pattern in Java?
- Why singleton class is always sealed in C#?
- What is the difference between Static class and Singleton instance in C#?
- How to write a singleton class in C++?
- How to use singleton class in android?
- What is the class "class" in Java?
- How we can create singleton class in Python?
- What is a static class in Java?
- What is a Throwable class in Java?
- What is a Locale class in Java?
- What is a Nested Class in Java?
- What is singleton design concept in PHP?
- How to make a singleton enum in Java?

Advertisements