Does the Java 'final' keyword actually improve security?

前端 未结 12 865
遥遥无期
遥遥无期 2021-01-07 19:07

While there are many reasons to use the \'final\' keyword in Java, one of the ones I keep hearing over and over again is that it makes your code more secure. While this seem

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 19:15

    It doesn't make your code more secure, it is more for thread safety than anything else. If a variable is marked final, a value must be assigned to it when the object is created. After object creation, that variable cannot be made to refer to another value.

    This behavior allows you to reason about the state of an object and make certain assumptions when multiple threads are accessing it concurrently.

提交回复
热议问题