Why shouldn't I use immutable POJOs instead of JavaBeans?

前端 未结 7 1325
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 06:27

I have implemented a few Java applications now, only desktop applications so far. I prefer to use immutable objects for passing the data around in the application instead of

相关标签:
7条回答
  • 2020-12-04 07:07

    I was surprised that the word Thread did not appear anywhere in this discussion.

    One of the main benefits of immutable classes is that they are inherently more thread safe due to no mutable, shared state.

    Not only does this make your coding easier, it'll also give you two performance benefits as a side effect:

    • Less need for synchronization.

    • More scope for using final variables, which can facilitate subsequent compiler optimisations.

    I am really trying to move towards immutable objects rather than JavaBean style classes. Exposing the guts of objects via getters and setters should probably not be the default choice.

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