Java changing value of final array element

后端 未结 4 696
长情又很酷
长情又很酷 2021-01-14 08:17

I am trying to understand what happens if I reassign a value to array element which is final, this is the code :

public class XYZ {
    private static final          


        
4条回答
  •  暖寄归人
    2021-01-14 08:54

    When final is applied to a variable, that only means that it cannot be reassigned once initialized. That does not prevent the object itself from being modified. In this case, you are modifying the array object. So this is fine:

    ABC[0] = " World!!!";
    

    But this would not have been allowed.

    ABC = new String[]{" World!!!"};
    

提交回复
热议问题