- Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathImmutableClass.java
More file actions
Latest commit
57 lines (45 loc) · 1.2 KB
/
Copy pathImmutableClass.java
File metadata and controls
57 lines (45 loc) · 1.2 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
57
packageobjects.immutableclass;
importjava.util.ArrayList;
importjava.util.List;
/**
* Make a class immutable.
* http://javarevisited.blogspot.com/2013/03/how-to-create-immutable-class-object-java-example-tutorial.html
* User: rpanjrath
* Date: 10/22/13
* Time: 4:02 PM
*/
publicfinalclassImmutableClass {
// make all variables final
privatefinalintanInt;
privatefinalStringstring;
// making list final won't make it immutable. You can use getList and then perform add on it.
privatefinalList<String> list;
publicImmutableClass(intanInt, Stringstring, List<String> list) {
this.anInt = anInt;
this.string = string;
this.list = list;
}
publicintgetAnInt() {
returnanInt;
}
publicStringgetString() {
returnstring;
}
publicList<String> getList() {
ListtempList = newArrayList();
tempList.addAll(list);
returntempList;
}
// REMOVE ALL SETTERS
/*
public void setString(String string) {
this.string = string;
}
public void setAnInt(int i) {
this.anInt = i;
}
public void setList(List list) {
this.list = list;
}
*/
}