How does the “final” keyword in Java work? (I can still modify an object.)

前端 未结 18 2721
醉酒成梦
醉酒成梦 2020-11-22 03:08

In Java we use final keyword with variables to specify its values are not to be changed. But I see that you can change the value in the constructor / methods of

18条回答
  •  清歌不尽
    2020-11-22 03:26

    Worth to mention some straightforward definitions:

    Classes/Methods

    You can declare some or all of a class methods as final, in order to indicate that the method cannot be overridden by subclasses.

    Variables

    Once a final variable has been initialized, it always contains the same value.

    final basically avoid overwrite/superscribe by anything (subclasses, variable "reassign"), depending on the case.

提交回复
热议问题