Why final keyword is necessary for immutable class

2019-01-16 15:08发布

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 attributes as private and final, then also it is an immutable class, isn't it?

Sorry if the question seems easy, but i am truly confused about it. Help me out.

Editted: I know that a class declared final can't be subclassed.. But if each attribute is private and final then what difference does that make?

7条回答
手持菜刀,她持情操
2楼-- · 2019-01-16 15:42

'final' as the keyword's name suggest means that the attribute to which final keyword is attached can't be changed(in terms of value) in other words it behaves like a constant.

As per your question if all members of the class is made private and final but the class is not made final then the same class can be inherited but the super class member are immutable as final keyword is attached to them.

查看更多
登录 后发表回答