jeudi 5 février 2015

How should I annotate, that a class is immutable in Java?


I recently stumbled upon how useful immutable objects are and that e.g. if you pass elements to a constructor and your class should be immutable, you have to copy these elements if they are not immutable themselves.


This requires a lot of checking or knowledge about my project, because if I have



public A(B foo)


and B is not immutable, A cannot be immutable either. Now imagine B seems immutable, but itself has mutable classes in the constructor and so on.


Is there a standard or best-practice for documenting if a class is immutable in Java? It seems there is no @immutable keyword in Javadoc.


The @Immutable annotation seems to be something totally different for auto class generation and not part of standard Java.





Aucun commentaire:

Enregistrer un commentaire