-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvenOddSingleton.java
More file actions
35 lines (31 loc) · 912 Bytes
/
EvenOddSingleton.java
File metadata and controls
35 lines (31 loc) · 912 Bytes
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
32
33
34
35
public class EvenOddSingleton {
private static EvenOddSingleton odd;
private static EvenOddSingleton even;
private String info;
private static boolean isOdd = true;
private EvenOddSingleton(String info) {
this.info = info;
}
public static EvenOddSingleton getInstance() {
if (isOdd) {
if (odd == null)
odd = new EvenOddSingleton("Odd");
isOdd = false;
return odd;
} else {
if (even == null)
even = new EvenOddSingleton("Even");
isOdd = true;
return even;
}
}
public String info() {
return info;
}
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
EvenOddSingleton instance = EvenOddSingleton.getInstance();
System.out.println(instance.info());
}
}
}