Immutable class means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable. We can create our own immutable class as well.
Following are the requirements:
- The class must be declared as final (So that child classes can’t be created)
- Data members in the class must be declared as final (So that we can’t change the value of it after object creation)
- A parameterized constructor
- Getter method for all the variables in it
- No setters(To not have the option to change the value of the instance variable)
Example to create Immutable class
// An immutable class public final class Student { final String name; final int regNo; public Student(String name, int regNo) { this.name = name; this.regNo = regNo; } public String getName() { return name; } public int getRegNo() { return regNo; } } // Driver class class Test { public static void main(String args[]) { Student s = new Student("ABC", 101); System.out.println(s.getName()); System.out.println(s.getRegNo()); // Uncommenting below line causes error // s.regNo = 102; } } |
ABC 101
In this example, we have created a final class named Student. It has two final data members, a parameterized constructor and getter methods. Please note that there is no setter method here.
(In order to make it functional, create objects of Student class in the main function.)
This article is contributed by Abhishree Shetty. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Factory method to create Immutable List in Java SE 9
- Factory method to create Immutable Set in Java 9
- Factory method to create immutable Map in Java 9
- Does JVM create object of Main class (the class with main())?
- Primitive Wrapper Classes are Immutable in Java
- Immutable List in Java
- Immutable Map in Java
- Immutable Set in Java
- Java: String is Immutable. What exactly is the meaning?
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Java | How to create your own Helper Class?
- How to create a Java HashMap of user defined class type?
- How to create a Class in JShell of Java 9
- Using predefined class name as Class or Variable name in Java
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples

