Had an office query relating to the use of the final
construct in Java for HashSet
, List
, Set
, etc.
Say you had a list of usernames, which you wanted to be updated. The list itself won't be reinitialized, just it's values changed.
The question was then raised whether the usernames variable should be a final
or not.
private static final List<String> USERNAMES = new List<String>();
...
USERNAMES.add("user1");
Or
private static List<String> usernames = new List<String>();
...
usernames.add("user1");
My question is, which, conventionally, would you use?
The argument can be posed that capitals, implying it's unmodifyable, is not valid here as you can modify the contents of the list (where you can't the list instance itself). Therefore, it's not actually a constant.
By the same token, it's valid to say the list
isn't modifyable, therefore capitals & final
is fine.
Thoughts? What would the Java conventions say?
I'm genuinely interested in the answer, and believe this is a question. Java conventions must have a stance or comment about this.