-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingletonClass.java
More file actions
36 lines (33 loc) · 1.03 KB
/
SingletonClass.java
File metadata and controls
36 lines (33 loc) · 1.03 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
class Singleton {
private static Singleton singleton = null;
private String str;
private Singleton(){
str = "vishal patil";
}
public static Singleton getSingletonInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
String getString(){
return str;
}
void setString(String str){
this.str = str;
}
}
public class SingletonClass{
public static void main(String[] args) {
Singleton a = Singleton.getSingletonInstance();
Singleton b = Singleton.getSingletonInstance();
Singleton c = Singleton.getSingletonInstance();
System.out.println("a.str --> "+a.getString());
System.out.println("b.str --> "+b.getString());
System.out.println("c.str --> "+c.getString());
b.setString("joker");
System.out.println("a.str --> "+a.getString());
System.out.println("b.str --> "+b.getString());
System.out.println("c.str --> "+c.getString());
}
}