-
Notifications
You must be signed in to change notification settings - Fork 1
/
MonostateSingleton.java
48 lines (39 loc) · 1.02 KB
/
MonostateSingleton.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Monostate
package creational.singleton;
class ChiefExecutiveOfficer
{
// Monostate by adding static modifier
private static String name;
private static int age;
public String getName() {
return name;
}
public void setName(String name) {
ChiefExecutiveOfficer.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
ChiefExecutiveOfficer.age = age;
}
@Override
public String toString() {
return "ChiefExecutiveOfficer{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class MonostateSingletonDemo
{
public static void main(String[] args) {
{
ChiefExecutiveOfficer ceo1 = new ChiefExecutiveOfficer();
ceo1.setName("Adam Smith");
ceo1.setAge(55);
ChiefExecutiveOfficer ceo2 = new ChiefExecutiveOfficer();
System.out.println(ceo2); // object is initialized with ceo1 info
}
}
}