Home > Tutorial > Java Singleton Class Sample Code

Java Singleton Class Sample Code

When I first started programming in Java, I soon realized that the concept of a global variable is not something that is supported in Java. A global variable in c or c++ was a familiar concept. While Java does not support a global variable, you can achieve something similar using a Java Singleton class.

There is no actual Java Singleton class to import, but there are methods within Java that enable you to create your own. One of the main benefits of a singleton in my mind is when dealing with Java multi-threading. A singleton class allows you to have a single instance of a class that allows sharing of static variables among many threads.

The sample Java code below is a basic sample of a Java Singleton class. The “global variable” being shared by potentially many threads is a global counter. The other variable is instance which is static and volatile based on how it’s used. Make all of the get or set methods synchronized and make sure to always call getInstance() when using the get or set methods.

Java Singleton Class Source Code:

package com.jcd.java.tutorials;

public class JavaSingletonClass 
{
	private static volatile JavaSingletonClass instance = null;
	private static int globalCounter = 0;
		
	public JavaSingletonClass() 
	{
		
	}
	
	public static synchronized JavaSingletonClass getInstance()  
	{
		
		if (instance == null) 
		{
			System.out.println("instance is NULL");	
			
			synchronized(JavaSingletonClass.class)
			{
				if (instance == null)
				{
					instance = new JavaSingletonClass();
				}
			}
		}
		else
		{
			System.out.println("instance is NOT NULL");
		}
	
		return instance;
	}
	
	//Public synchronized method to set global counter
	public static synchronized void setGlobalCounter(int val) 
	{
		globalCounter = val;
	}
	
	//Public synchronized method to get global counter
	public static synchronized int getGlobalCounter() 
	{
		return globalCounter;
	}	
	
	//Public synchronized method to increment global counter
	public static synchronized void incrementGlobalCounter() 
	{
		globalCounter++;
	}
	
}


Instantiate Java Singleton Class:

JavaSingletonClass javaSingletonClass = new JavaSingletonClass();
javaSingletonClass.getInstance().setGlobalCounter(10);
javaSingletonClass.getInstance().incrementGlobalCounter();
javaSingletonClass.getInstance().incrementGlobalCounter();
javaSingletonClass.getInstance().incrementGlobalCounter();
System.out.println("Global Counter="+javaSingletonClass.getInstance().getGlobalCounter());


Java Singleton Class Output:

instance is NULL
instance is NOT NULL
instance is NOT NULL
instance is NOT NULL
instance is NOT NULL
Global Counter=13


What is a Volatile Variable?

A volatile variables reduces the risk of memory consistency errors. Any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. Therefore, changes to a volatile variable are always visible to other threads in terms of thread safety. In addition, it also means that when a thread reads a volatile variable, it sees the latest change to the volatile and the side effects of the code that led up the change.



Java References:

  1. Java Synchronized Method
  2. Java Atomic Access
Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot