Final variable manipulation in Java

前端 未结 11 776
无人及你
无人及你 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:45

    You can manipulate mutable final variables for e.g. of type StringBuffer but you cannot manipulate final variables of immutable types.

    In case of mutable variables, new object is not created every time it's value is changed. But in case of of immutable types, whenever you change value, new object is created, so when you make it final, you cannot modify it.

提交回复
热议问题