The following code in Java uses the final String array, and there is no doubt about it.
final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) { for (int x = 0; x < CONSTANT_ARRAY.length; x++) { System.out.print(CONSTANT_ARRAY[x] + " "); } } }
It displays the following output on the console.
I can never change
The following code also raises no questions.
final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) {
Obviously, a commented-out string causes the indicated error because we are trying to reassign the declared final array of type String .
How about the following code.
final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) { CONSTANT_ARRAY[2] = "always";
and displays I can always change means that we were able to change the value of the final array of type String . Can we ever change the entire array this way without breaking the final rule?
java
Lion Apr 26 '12 at 19:18 2012-04-26 19:18
source share