Final variable manipulation in Java

前端 未结 11 780
无人及你
无人及你 2020-11-28 23:28

Could anyone please tell me what is the meaning of the following line in context of Java:

final variable can still be manipulated unless it\'s immut

11条回答
  •  误落风尘
    2020-11-28 23:55

    You can call any method on it even if the method can change the state of the object the reference is pointing to. E.g

    final MyClass myClass = new MyClass();
    myClass.setVar(something);
    

    This is fine because myClass itself is not changing, i.e you are not doing myClass = myClass1;.

提交回复
热议问题