So from my understanding of final you can't change the value of variable after. In the case of a private final foo HashSet<>() = new HashSet<>() I can use foo as a HashSet and edit the values in it but I can't reassign foo right?
Asked
Active
Viewed 1,537 times
0
ford prefect
- 6,478
- 10
- 54
- 83
1 Answers
6
final will prevent you to assign any new value to the variable, but it won't make your object immutable.
In your example,
private final foo HashSet<String>() = new HashSet<>();
foo.put("bar"); // is correct
foo = new HashSet<>(); // will fail at compilation
dotvav
- 2,726
- 13
- 30