This is one of the review question in Khalid Mughal.
Question:
Which statements would cause a compilation error if inserted in the location indicated in the following program?
public class ParameterUse {
static void main(
String[] args) {
int a = 0;
final int b = 1;
int[] c = { 2 };
final int[] d = { 3 };
useArgs(a, b, c, d);
}
static void useArgs(final int a, int b, final int[] c, int[] d) {
// INSERT STATEMENT HERE.
}
}
Select the two correct answers.
a) a++;
b) b++;
c) b = a;
d) c[0]++;
e) d[0]++;
f) c = d;
The answer is a and f. I am wondering why it is not d.
Here is the explanation in the book for the answers however i am not convinced. Can anyone please help me here.
Values can only be assigned once to final variables. A final formal parameter is assigned the value of the actual parameter at method invocation. Within the method body, it is illegal to reassign or modify the value of a final parameter. This causes a++ and c = d to fail. Whether the actual parameter is final does not constrain the client that invoked the method, since the actual parameter values are copied to the formal parameters.