Stats

27 Contributors: 1 Friday, June 16, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Immutable Class

Introduction

Immutable objects are instances whose state doesn’t change after it has been initialized. For example, String is an immutable class and once instantiated its value never changes.

Remarks

Some immutable classes in Java:

  1. java.lang.String
  2. The wrapper classes for the primitive types: java.lang.Integer, java.lang.Byte, java.lang.Character, java.lang.Short, java.lang.Boolean, java.lang.Long, java.lang.Double, java.lang.Float
  3. Most enum classes are immutable, but this in fact depends on the concrete case.
  4. java.math.BigInteger and java.math.BigDecimal (at least objects of those classes themselves)
  5. java.io.File. Note that this represents an object external to the VM (a file on the local system), which may or may not exist, and has some methods modifying and querying the state of this external object. But the File object itself stays immutable.

Related Examples