-3

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

ygngy
  • 3,630
  • 2
  • 18
  • 29
Wru
  • 13
  • 3
  • Please make your question clear and you'd better paste your code here . – Frank AK Mar 28 '18 at 09:45
  • That is invalid Java code –  Mar 28 '18 at 12:14
  • it seems you want `final` which cannot be modified. – Rishal Mar 28 '18 at 12:16
  • What do you mean by "permanent"? It's very unclear what you're asking at the moment, I'm afraid. – Jon Skeet Mar 28 '18 at 12:18
  • Please write a valid code! – Ankit Mehta Mar 28 '18 at 12:18
  • Permanent like store in database or file? – Piro Mar 28 '18 at 12:27
  • u mean [immutable](https://en.wikipedia.org/wiki/Immutable_object) ? – Sikorski Mar 28 '18 at 12:31
  • I just want a large number of strings that can’t be removed – Wru Mar 28 '18 at 12:46
  • Removed from what? We can't read in your mind. Edit your question, and elaborate. – JB Nizet Mar 28 '18 at 13:25
  • I suspect the best way would be to make it private and have your code not update any of the values. Setting it to final will not stop you from overwriting values, only from changing it to be a different array. If other classes rely on that array, you could create a method `String[] getArr(){return {"a", "b", "c"};}` – phflack Mar 28 '18 at 14:10
  • What is the overall goal? It's easy to never change the value in your code, you would not need to lock it locally – phflack Mar 28 '18 at 14:11
  • @phflack yes you can but you can not control other developers, can you? Even you yourself may change it accidentally! – ygngy Mar 28 '18 at 14:17
  • @Shadow You can lock down classes you create, but if the other developer has direct access to all of your code, then they can just edit out the locks. Trying to control other developers is probably off-topic for this site, but may be more apt for [Workplace](https://workplace.stackexchange.com/)/[IPS](https://interpersonal.stackexchange.com/) – phflack Mar 28 '18 at 14:19
  • @phflack We want values to be locked otherwise what is Application of `final` or similar key words in other languages like `const`. yes any one can edit code intentionally but developer can protect its values from his mistakes and others mistakes by locking it to ease debugging. – ygngy Mar 28 '18 at 14:30
  • If you are always accessing the Strings individually, it may be better to give them each names and declare each as `private static final String STRINGNAME = "String";` – phflack Mar 28 '18 at 14:33
  • If it is a list of Strings that you need locked, you could use a final unchangeable list – phflack Mar 28 '18 at 14:34
  • How do you do that? – Wru Mar 28 '18 at 15:19

1 Answers1

0

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

phflack
  • 2,729
  • 1
  • 9
  • 21