Skip to content

Instantly share code, notes, and snippets.

@lol97
Created April 15, 2024 15:57
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 lol97/98a1a7042b798a4efd2eb9672c66e8fe to your computer and use it in GitHub Desktop.
Save lol97/98a1a7042b798a4efd2eb9672c66e8fe to your computer and use it in GitHub Desktop.
example lazy Instantiation singleton with java
package sufyan97_blog.design_pattern;
class ConfigLazy {
private static ConfigLazy configLazy;
private ConfigLazy() {
}
public static ConfigLazy getConfigLazy() {
if(configLazy == null) {
synchronized (SingletonLazy.class) {
if(configLazy == null) {
configLazy = new ConfigLazy();
}
}
}
return configLazy;
}
}
public class SingletonLazy {
public static void main(String[] args) {
System.out.println(ConfigLazy.getConfigLazy().hashCode());
System.out.println(ConfigLazy.getConfigLazy().hashCode());
System.out.println(ConfigLazy.getConfigLazy().hashCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment