Why final keyword is necessary for immutable class?

后端 未结 7 1238
陌清茗
陌清茗 2020-12-08 01:17

Could you please clarify that why final keyword is required before class when we are making it an immutable one. I mean, if we declare all of it\'s attr

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 02:10

    A class that is declared final cannot be subclassed. See also http://docs.oracle.com/javase/tutorial/java/IandI/final.html

    The different semantics of all uses of the final keyword are described in the The Java Language Specification

    • 4.12.4 final Variables Page 80
    • 8.1.1.2 final Classes Page 184
    • 8.3.1.2 final Fields Page 209
    • 8.4.3.3 final Methods Page 223

提交回复
热议问题