Create Immutable Class in Java | Advantages of immutable class in java What is immutable class or object? In simple term, once the object of the class created its fields cannot be changed or modified. Like once Student object is created, it can not be changed. In java, all wrapper classes are immutable. like Boolean, String, Integer, Double, Long, Float. Why we need Immutable class? or Advantages of Immutable class When we does not want to change its fields after created. It is Thread safe. These objects are good for use as hash key (HashMap) Reference of immutable objects can be cached. How to create Immutable Class in Java? We can create Immutable class using simple following steps : Make class Final Make class fields Private and Final Do not write Setter method for fields Create all arguments constructor Using above steps we can create Immutable class in Java. So lets go on code... // Final class final class Company { // Private fields final private int id; final
Welcome To Programming Tutorial. Here i share about Java Programming stuff. I share Java stuff with simple examples.