How do I identify immutable objects in Java

后端 未结 15 1341
小蘑菇
小蘑菇 2020-11-30 22:40

In my code, I am creating a collection of objects which will be accessed by various threads in a fashion that is only safe if the objects are immutable. When an attempt is m

15条回答
  •  [愿得一人]
    2020-11-30 23:06

    At my company we've defined an Attribute called @Immutable. If you choose to attach that to a class, it means you promise you're immutable.

    It works for documentation, and in your case it would work as a filter.

    Of course you're still depending on the author keeping his word about being immutable, but since the author explicitly added the annotation it's a reasonable assumption.

提交回复
热议问题