You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * A thread-safe Singleton implementation using the static inner class pattern. * * This is one of the most widely recommended and robust approaches. It combines * the advantages of lazy initialization with thread safety without requiring * explicit synchronization. * * How it works: * 1. The `Singleton` class is a normal class. * 2. The `SingletonHelper` class is a static inner class. It is not loaded * into memory until the `getInstance()` method is called for the first time. * 3. When `SingletonHelper` is loaded, the static final instance `INSTANCE` is * created. The JVM guarantees that static initialization is thread-safe. * 4. Subsequent calls to `getInstance()` will simply return the existing * `INSTANCE` without any synchronization overhead. */publicclassSingleton {
// The constructor is private to prevent direct instantiationprivateSingleton() {
System.out.println("Singleton instance created.");
}
// The static inner class that holds the singleton instanceprivatestaticclassSingletonHelper {
privatestaticfinalSingletonINSTANCE = newSingleton();
}
// The public method to get the singleton instancepublicstaticSingletongetInstance() {
returnSingletonHelper.INSTANCE;
}
// Example method to show it's workingpublicvoidshowMessage() {
System.out.println("Hello from the singleton!");
}
// Main method to test the singletonpublicstaticvoidmain(String[] args) {
// Get the first instanceSingletoninstance1 = Singleton.getInstance();
instance1.showMessage();
// Get the second instance (it should be the same object)Singletoninstance2 = Singleton.getInstance();
instance2.showMessage();
// Check if both references point to the same objectSystem.out.println("Are instance1 and instance2 the same object? " + (instance1 == instance2));
}
}