3

why is this code working?

public class Final {
    public final int[][] arr = new int[2][];
    Final(){
        arr[0] = new int[4];
        arr[0] = new int[5];
        arr[1] = new int[4];
        }
    public static void main(String[] args) {
        Final fi = new Final();
        System.out.println(fi.arr[0].length);
        System.out.println(fi.arr[1].length);
    }
}

whereas the following does not work (which is correct as I assume).

public class Final {
    public final int[] arr;
    Final(){
        arr = new int[4];
        arr = new int[5];
        }
    public static void main(String[] args) {
        Final fi = new Final();
        System.out.println(fi.arr.length);
    }
}

When is the final statement "kicking in" ?

epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
  • Makes me wonder, how _do_ you make an all final multidimensional array in Java? – SQB Nov 07 '13 at 22:12
  • @SQB You do not work a lot with arrays in Java. Some kind of (immutable) collection or other data structures fit better in most cases. http://stackoverflow.com/questions/3700971/immutable-array-in-java – Fabian Barney Nov 08 '13 at 20:38
  • @FabianBarney I agree, but I'd still like to know. – SQB Nov 08 '13 at 20:42
  • @SQB You can't. There is no way in having immutable arrays in Java. :) – Fabian Barney Nov 08 '13 at 20:43

4 Answers4

5

The final keyword indicates that the value of the variable will not change once it's initialized. That makes the most sense for primitives, where final int = 5; means it can't be reassigned 6.

For reference variables, it means that it can't be re-assigned to another reference, because the value is the reference to an object. But it doesn't stop you from modifying the contents of the array. It just stops you from doing another assignment to the reference variable. That's why the second piece of code doesn't work -- you assign it another object after it was already assigned for the first time.

rgettman
  • 176,041
  • 30
  • 275
  • 357
3

You are not modifying the basket, but rather the apples inside.

The instance of int[][] never changes, just the content inside of it.

Obicere
  • 2,999
  • 3
  • 20
  • 31
2

The fact that arr is marked final just means you can't make arr reference a different value. You're still completely free to modify the contents of the array.

So that means that this is not allowed:

arr = new int[4];
arr = new int[5]; //Reassigning a final variable that's already initialized- bad!

but this is fine:

 arr[0] = new int[4]; 
 arr[0] = new int[5];
 arr[1] = new int[4]; //We haven't changed the array that arr is pointing to
musical_coder
  • 3,886
  • 3
  • 15
  • 18
2

Arrays are objects in Java. Object-Variables are in fact references to an object. Final variables or final fields are references that cannot point to another object after initialization. The referenced object itself can be modified. You've to distinguish the reference and the referenced object.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60