Immutable class in java

后端 未结 3 989
傲寒
傲寒 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:10

    Yes, still it can be modified through reflection. Apart from that it seems you took required care to make it immutable.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 05:21

    There's no hiding from reflection - even immutable classes are not immune. There is nothing you can do about it, though, so "cannot be modified through reflection" is not one of the criteria of immutability.

    0 讨论(0)
提交回复
热议问题