-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmutableClass.java
More file actions
31 lines (26 loc) · 932 Bytes
/
immutableClass.java
File metadata and controls
31 lines (26 loc) · 932 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
import java.util.ArrayList;
import java.util.List;
final class myimmutable{
private final String name;
private final List<Object> petNameList;
myimmutable(String name, List<Object> perNameList){
this.name = name;
this.petNameList = perNameList;
}
public String getName(){
return name;
}
public List<Object> getPetNamObjects(){
return new ArrayList<>(petNameList);
}
}
class immutableClass{
public static void main(String[] args) {
List<Object> petname = new ArrayList<>();
petname.add("dog");
petname.add("goat");
myimmutable obj = new myimmutable("sai", petname);
obj.getPetNamObjects().add("testing");//in result why this testing not showing because in sout i jusr obj.getpetname so this call new ArrayList<>(petNameList); alrady stored data in petnamelist
System.out.println(obj.getPetNamObjects());
}
}