-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathSingletonTest.java
31 lines (25 loc) ยท 934 Bytes
/
SingletonTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class SingletonTest {
public static void main(String[]args) {
SingletonClass sclass = SingletonClass.getInstance();
SingletonClass sclass2 = SingletonClass.getInstance();
System.out.println(sclass);
System.out.println(sclass2);
// sclass, sclass2 ๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด ๊ทธ ์ฃผ์๊ฐ์ด ๊ฐ์ ๊ฒ์ ํ์ธํ ์ ์๋ค.
NonSingletonClass nclass = new NonSingletonClass();
NonSingletonClass nclass2 = new NonSingletonClass();
System.out.println(nclass);
System.out.println(nclass2);
// nclass, nclass2 ๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด ์ฃผ์๊ฐ์ด ๋ค๋ฅด๋ค.
}
}
class SingletonClass {
private SingletonClass() {}
static { sClass = new SingletonClass(); }
private static SingletonClass sClass;
public static SingletonClass getInstance() {
return sClass;
}
}
class NonSingletonClass {
public NonSingletonClass() {}
}