Andreas Petersson Andreas Petersson - 1 year ago 66
Java Question

Complete List of immutable JDK classes?

is there a list of de-facto immutable classes in the jdk?

technically Immutable classes include the obvious Integer, Double etc..

de-facto immutable will include for example java.lang.String - it might technically be mutable but de-facto it is not.

Also, are there Interfaces/Abstract classes which are required (as stated in the javadoc) to be immutable?

if you cannot provide a complete List, i would already be happy if you know a bunch of classes which state immutability in its javadoc..

Answer Source

Classes whose object contents cannot be modified is called immutable classes. All primitive data types(Wrapper classes only) are immutable. For any class to be immutable the following needs to be done.

  • Make all fields private
  • Don't provide mutators
  • Ensure that methods can't be overridden by either making the class final (Strong Immutability) or making your methods final (Weak Immutability)
  • If a field isn't primitive or immutable, make a deep clone on the way in and the way out.

Thank you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download