-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSingletonExample.java
More file actions
56 lines (36 loc) · 1.63 KB
/
SingletonExample.java
File metadata and controls
56 lines (36 loc) · 1.63 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class SingletonExample {
public static void main(String[] args) {
String strCompare = " This is the value of my local String variable and it will be the same as the toString of the Singleton,";
MySingleton singletonVariable1 = MySingleton.getSingleInstance();
System.out.println(singletonVariable1.toString() +" which has hashcode: "+ singletonVariable1.hashCode());
MySingleton singletonVariable2 = MySingleton.getSingleInstance(); //trying to get another instance, but there is only one
System.out.println("I tried to get another instance and assign it to singletonVariable2, but you can see the hashcode is the same: "+ singletonVariable2.hashCode());
if (singletonVariable2.toString().equals(strCompare)){
System.out.print("The value is the same when I compare with toString().equals,");
if (singletonVariable2.toString() == strCompare){
System.out.println(" but it is not the same object when I compared using '==', \nbecause my local String's hashcode is : " + strCompare.hashCode());
}
}
}
}
class MySingleton {
private static volatile MySingleton mySingleton;
public String str = " This is the value of my local String variable and it will be the same as the toString of the Singleton,";
private MySingleton() {
}
public static MySingleton getSingleInstance() {
//double-checked locking
if (null == mySingleton) {
synchronized (MySingleton.class) {
if (null == mySingleton){
mySingleton = new MySingleton();
}
}
}
return mySingleton;
}
@Override
public String toString() {
return str;
}
}