Skip to content

Instantly share code, notes, and snippets.

@evrentan
Last active August 18, 2022 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrentan/ecadd26c90c69a1c3fdef1a9a590e593 to your computer and use it in GitHub Desktop.
Save evrentan/ecadd26c90c69a1c3fdef1a9a590e593 to your computer and use it in GitHub Desktop.
Eager Instantiation of Singleton Design Pattern in Java
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