Created
April 15, 2024 15:57
-
-
Save lol97/98a1a7042b798a4efd2eb9672c66e8fe to your computer and use it in GitHub Desktop.
example lazy Instantiation singleton with 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 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