Javatpoint Logo
Javatpoint Logo

Nested Initialization for Singleton Class in Java

Nested initialization is a technique in Java used to implement a singleton class, which is a class that allows only one instance to exist at any given time. The nested initialization technique ensures that only one singleton class instance is created, even if multiple threads try to create it.

The nested initialization technique creates a nested class within the singleton class. Within the nested class, there exists a static variable that serves as a container for the instance of the singleton class. When the nested class is first created, it initializes the static variable with a new singleton class instance. It ensures that just one instance of the singleton class is produced.

The following steps must be followed in Java to implement a nested initialization singleton class:

  1. Make the Constructor of the class private. By utilizing nested initialization, other classes are prevented from directly creating instances of the singleton class.
  2. Create a nested class inside the singleton class. The nested class will be responsible for holding the singleton instance.
  3. Initialize the singleton instance in the nested class's static initializer block. The nested initialization technique in Java ensures that the instance of a singleton class is created only once during the loading of the nested class. It blocks the creation of numerous instances.
  4. Build a static method that is public and returns the singleton instance. Other classes will use the Method to access the singleton instance.

Demonstrate Singleton Class using Nested Initialization

Filename: Singleton.java

Output:

Hello World!

Advantages of Nested Initialization:

  • Lazy loading
  • Thread-Safety

Lazy loading

Lazy loading is a way of creating objects only when they are needed. It can improve performance because the program doesn't have to do as much work when it starts up. When the getInstance() method is used for the first time in a singleton class, the object is only then constructed.

Thread-safety

Thread safety is a critical concern in multi-threaded programs. Multiple threads can execute a thread-safe program without causing data corruption or other problems. The nested initialization pattern is thread-safe because the Singleton class object is created and initialized only once, regardless of how many threads are trying to access it.

A non-Thread-Safe implementation of the Singleton class

Filename: MySingleton.java

Output:

Hello, I am a Singleton object!
Updated Singleton message

Thread-Safe implementation of Singleton class

Filename: NestedInitializationDemo.java

Output:

Singleton instance created
Thread 1: It is a singleton value
Thread 2: It is a singleton value

Next TopicObject in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA