Singleton Design pattern in JAVA

Below class depicts the Singleton design pattern.

package in.javatutorials;

* @author JavaTutorials
* @version 1.0
public final class SingletonDesignPattern {

* make the class object as private as it is
* not used directly out side the class
private static SingletonDesignPattern singletonObj = null;

* make the constructor as private.
* Object cannot be created using the constructor outside of this class.
private SingletonDesignPattern(){


* Create the class object if it is null and
* @return singletonObj SingletonDesignPattern
public static SingletonDesignPattern getInstance(){
if(singletonObj == null){
singletonObj = new SingletonDesignPattern();
return singletonObj;

* Take the user name as parameter and return the welcome message
* @param userName String
* @return message String
public String getWelcomeInfo(String userName){
String message = null;
//make method access to synchronized to make thread safe
synchronized (SingletonDesignPattern.class) {
System.out.println(“User Name is : ” + userName);
message = “Hello ” + userName + “!!!”;
return message;

The main() method of below class will get the single ton object and access the other methods using the same.

package in.javatutorials;

public class TestSingleton {

public static void main(String[] args) {
//Get the singleton object
SingletonDesignPattern singletonObj = SingletonDesignPattern.getInstance();




I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , , , , , , , ,
Posted in JAVA
4 comments on “Singleton Design pattern in JAVA
  1. Enums are the new way to create singleton pattern. See a comparison of various ways to create singleton at:
    Singleton design pattern in Java

  2. […] Singleton Design pattern in JAVA […]

Leave a Reply to Java tutorials Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 679 other followers

December 2012
« Nov   Jan »
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 324,998 hits