If I have
String[] arr = new String[6]
what code can I use to make it permanent? I haven’t tried anyway way yet and I’m finding it difficult to get this specific information.
Thanks
If I have
String[] arr = new String[6]
what code can I use to make it permanent? I haven’t tried anyway way yet and I’m finding it difficult to get this specific information.
Thanks
The main issue with arrays is that they are hard to lock
Using final String[] arr = {"a", "b"};
blocks attempts to set arr
to a new value, but using arr[0] = "c";
is still valid
Instead, it's easier to just keep it private and never change it in your code
If this list is to be sent to other places and must not be modified, you can use an unmodifiableList object instead
List<String> arr = new ArrayList<String>();
arr.add("a");
arr.add("b");
arr = Collections.unmodifiableList(arr);
It isn't completely safe, as reflection will still be able to modify the values (example), but it should suffice for avoiding errors while coding