Reputation: 1597
class hello {
public static void main(String arg[]){
int[] c = { 2 };
final int[] d = { 3 };
}
static void useArgs(final int a, int b, final int[] c, int[] d) {
c[0]=d[0]; // no error
c = d; //error
}
}
guys can anybody can explain this behavior?
Upvotes: 2
Views: 151
Reputation: 6969
Variable c
is final. Which means that you cannot assign another value to that variable.
But the elements in the array itself are not final, which is why you are able to change the assignment on the elements like c[0]=d[0]
.
Upvotes: 6
Reputation: 13713
c is a final (const) reference to an array of ints. and since c is final you cannot change its value (i.e change the address it refers to). And this goes for any variable declared as final (not just arrays).
This also won't work :
final int c = 1;
int d = 2;
c = 2; // Error
c = d; // Error
Upvotes: 3