Last active
August 18, 2022 08:32
-
-
Save evrentan/ecadd26c90c69a1c3fdef1a9a590e593 to your computer and use it in GitHub Desktop.
Eager Instantiation of Singleton Design Pattern in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.dto; | |
import java.time.LocalTime; | |
public class EagerInstantiationOfSingletonClass { | |
/** | |
* Attribute that provides the eager instantiation of the class while load time and as it is static, it will be loaded once | |
*/ | |
private static EagerInstantiationOfSingletonClass eagerInstantiationOfSingletonClass = new EagerInstantiationOfSingletonClass(); | |
/** | |
* Private constructor to avoid instantiate the class from outside the class | |
* | |
* @author <a href="https://github.com/evrentan">Evren Tan</a> | |
*/ | |
private EagerInstantiationOfSingletonClass() { | |
System.out.println(String.format("%s is instantiated at call time at %s !!!", EagerInstantiationOfSingletonClass.class.getCanonicalName(), LocalTime.now())); | |
} | |
/** | |
* Static factory method that returns the object instantiated during load time | |
* @return EagerInstantiationOfSingletonClass instance | |
* | |
* @author <a href="https://github.com/evrentan">Evren Tan</a> | |
*/ | |
public static EagerInstantiationOfSingletonClass getInstance() { | |
return eagerInstantiationOfSingletonClass; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment