Skip to main content

Singleton Class using Private Constructor Example in Java

2 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Singleton Class Using Private Constructor is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Syntax

class ClassName
{
    static ClassName instance = null;
   
    private ClassName() {  }
  
    static public ClassName getInstance()
    {
        if (instance == null)        
             instance = new Singleton();
  
        return instance;
    } 
}

//Usage
ClassName instance = ClassName.getInstance();

Singleton Class Example Program

class Singleton
{
    static Singleton instance = null;
    public int instanceValue = 10;
   
    private Singleton() {  }
  
    static public Singleton getInstance()
    {
        if (instance == null)        
             instance = new Singleton();
  
        return instance;
    } 
}

public class SingletonClass {
    public static void main(String args[])    
   {
       Singleton instance1 = Singleton.getInstance();
       Singleton instance2 = Singleton.getInstance();
       
       //Objects are different
       //But same instance will be used
       instance1.instanceValue = instance1.instanceValue + 10;
       System.out.println("Value of instance1.instanceValue = " + instance1.instanceValue);
       System.out.println("Value of instance2.instanceValue = " + instance2.instanceValue);
   }    
}

Sample Output

Value of instance1.instanceValue = 20
Value of instance2.instanceValue = 20

When to use

Use this singleton class using private constructor example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. static Singleton instance = null; updates a variable used in the calculation or output.

  3. The if statement runs the nested code only when the condition is true.

  4. instance = new Singleton(); updates a variable used in the calculation or output.

  5. Singleton instance1 = Singleton.getInstance(); updates a variable used in the calculation or output.

  6. Singleton instance2 = Singleton.getInstance(); updates a variable used in the calculation or output.

  7. instance1.instanceValue = instance1.instanceValue + 10; updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — delegate work to other methods as programs grow.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Singleton Class Using Private Constructor program demonstrate?
It shows how to implement singleton class using private constructor in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials