Skip to content

Commit 44b6b0f

Browse files
committed
Add eagerly singleton
1 parent 012da33 commit 44b6b0f

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class EagerlySingleton
2+
{
3+
// Use static initiallizer, that will make instace created fast.
4+
private static EagerlySingleton instance = new EagerlySingleton();
5+
6+
private EagerlySingleton()
7+
{
8+
}
9+
10+
// Just get the instance
11+
public static EagerlySingleton getInstance()
12+
{
13+
return instance;
14+
}
15+
16+
public static void main(String[] args)
17+
{
18+
EagerlySingleton singletonInstance1 = EagerlySingleton.getInstance();
19+
EagerlySingleton singletonInstance2 = EagerlySingleton.getInstance();
20+
21+
if(singletonInstance1 == singletonInstance2)
22+
{
23+
System.out.println("They're the same instance");
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)