Immutable class in java

后端 未结 3 995
傲寒
傲寒 2020-12-19 04:15

I made my class immutable by following all java standards

A. Defined class as final
B. declared all fields as private and final
C. No setter method
D. No met         


        
3条回答
  •  无人及你
    2020-12-19 05:19

    Yes. Reflection can still access / change it. You can't really plan against that. If someone's altering your object with reflection, I would doubt the quality of code they're writing.

    Immutable classes are fantastic to ensure thread safe applications. Immutable objects are ALWAYS thread safe. If you're looking for more great information, please read Effective Java. It's a MUST READ for any Java developer.

提交回复
热议问题