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

前端 未结 18 2769
醉酒成梦
醉酒成梦 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:29

    1. Since the final variable is non-static, it can be initialized in constructor. But if you make it static it can not be initialized by constructor (because constructors are not static).
    2. Addition to list is not expected to stop by making list final. final just binds the reference to particular object. You are free to change the 'state' of that object, but not the object itself.

提交回复
热议问题