Search code examples
javamethodsparameter-passingfinal

final keyword preceding the parameter declaration in method declaration


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?


Solution

  • 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].