简体   繁体   English

方法声明中参数声明之前的final关键字

[英]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? 伙计们可以解释一下这种行为吗?

Variable c is final. 变量c是最终的。 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] . 但是数组本身的元素不是final,这就是为什么你能够改变c[0]=d[0]等元素的赋值。

c is a final (const) reference to an array of ints. c是对int数组的最终(const)引用。 and since c is final you cannot change its value (ie change the address it refers to). 并且由于c是最终的,你不能改变它的值(即改变它所指的地址)。 And this goes for any variable declared as final (not just arrays). 这适用于声明为final的任何变量(不仅仅是数组)。

This also won't work : 这也行不通:

final int c = 1;
int d = 2;
c = 2; // Error
c = d; // Error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM