Java changing value of final array element

后端 未结 4 695
长情又很酷
长情又很酷 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:48

    The final keyword does not mean you cannot change the state of the object, but only that the reference marked as final will remain constant. For example, you won't be able to do things like

    final String[] ABC = {"Hello"};
    ABC = new String[3]; // illegal
    ABC = someOtherArray; // illegal;
    ABC[0] = "World"; // legal
    

提交回复
热议问题