Home Java Overview Maven Tutorials

Wednesday, 26 April 2017

Singleton Class in Java with example

For Video Tutorial : Move on Youtube Channel

Note : Select the playlist as per your need & move with number sequence

For Video : CLICK HERE

What is singleton class ?
Singleton means one, that means you can create only one instance of a class.
If you are allowing a class to create only one instance that means class is singleton.
"Singleton" is not a keyword , it is a concept in java.
The Main purpose of he Singleton is to control object creation of a class.
How it can be acheived?
By creating a static object, private constructor of class and a method with return statement for object.
Step 1. We need to create constructor as private so that we can not create an object outside of the class.
Step 2. We need to create object with static keyword.

Code :

public class SingletonA {
public static void main(String[] args)
MainSingleton obj1 = MainSingleton.getobject();
class MainSingleton
// static MainSingleton obj = new MainSingleton(); this object will be created when your class will be loaded.
static MainSingleton obj;
private MainSingleton()
public static MainSingleton getobject()
if(obj ==null)
obj = new MainSingleton();
return obj;
public void prt()

Tags : Singleton class in java, what is singleton class in java, Singleton class with example


  1. Thank you for sharing this article.
    Social Media Marketing for Pharmacy Click here

  2. We are aware that for any product to arrive safely, it is very essential to have its ideal packaging and if the packaging is no longer excellent for any product then it can be dangerous to the product. That’s why we packaging every and each and every product very cautiously and arrive at the product to the purchaser safely and assist them to continue to be healthy. Make an order with a pharmacy order.

    Visit us - Pharmacy Oder Online packaging and shipping service