There are certain guidelines given below :
- Do not allow subclasses to override methods for Immutable Classes.
- Special attention when having mutable instance variables for Immutable Classes.
- Class is declared final so it cannot be extended.
- Do not provide setter methods for Immutable Classes.
- Class members name and date are private so cannot be accessed outside of class.
- Does not contain any setter methods so outer class cannot change the class members.
- Make all fields final and private for Immutable Classes.
- Getter Method returns the copy of class members.
- Class members are initialized using the constructor.
List of Immutable Classes in Java :
S.No. | Class Name | Package | S.No. | Class Name | Package |
---|---|---|---|---|---|
1 | String | java.lang | 2 | Integer | java.lang |
3 | Byte | java.lang | 4 | Character | java.lang |
5 | Short | java.lang | 6 | Boolean | java.lang |
7 | Long | java.lang | 8 | Double | java.lang |
9 | Float | java.lang | 10 | StackTraceElement | java.lang |
11 | BigInteger | java.math | 12 | BigDecimal | java.math |
13 | File | java.io | 14 | Font | java.awt |
15 | BasicStroke | java.awt | 16 | Color | java.awt |
17 | GradientPaint | java.awt | 18 | LinearGradientPaint | java.awt |
19 | RadialGradientPaint | java.awt | 20 | Cursor | java.awt |
21 | Locale | java.util | 22 | UUID | java.util |
23 | Collections | java.util | 24 | URL | java.net |
25 | URI | java.net | 26 | Inet4Address | java.net |
27 | Inet6Address | java.net | 28 | InetSocketAddress | java.net |
Create Immutable Class
package com.jaladhi.jst; public final class Immutable {private String name;
private int date;
Immutable(String name, int date) {
this.name = name;
this.date = date;
}
public String getName() {
return name;
}
public int getDate() {
return date;
}
}
package com.jaladhi.jst;
public class ImmutableClass {
public static void main(String args[]) {
Immutable obj = new Immutable("Jaladhi Soft Technology", 2020);
System.out.println("Name: " + obj.getName());
System.out.println("Date : " + obj.getDate());
}
}
Name : Jaladhi Soft Technology
Date : 2020
Date : 2020
package com.jaladhi.jst;
import java.util.Date;
public final class ImmutableDemo {
private final Integer intValue;
private final String stringValue;
private final Date dateValue;
private ImmutableClass(Integer intValue, String stringValue, Date dateValue) {
this.intValue = intValue;
this.stringValue = stringValue;
this.dateValue = new Date(date.getTime());
}
public static ImmutableDemo createNewInstance(Integer intValue, String stringValue, Date dateValue) {
return new ImmutableDeo(intValue, stringValue, dateValue);
}
public Integer getIntValue() {
return intValue;
}
public String getStringValue() {
return stringValue;
}
public Date getDateValue() {
return new Date(dateValue.getTime());
}
@Override
public String toString() {
return intValue +" - "+ stringValue +" - "+ dateValue;
}
}
public class ImmutableDemoTest {
public static void main(String[] args) {
ImmutableDemo id = ImmutableDemo.createNewInstance(1000,"Jaladhi Soft Technology", new Date());
System.out.println(id);
tryToChange(id.getIntValue(),id.getStringValue(),im.getDateValue());
System.out.println(id);
}
private static void tryToChange(Integer intValue, String stringValue, Date dateValue) {
intValue = 10000;
stringValue = "Jaladhi Soft Technology (JST).";
dateValue.setDate(30);
}
}
1000 - Jaladhi Soft Technology - Sat Dec 12 10:00:00 IST 2020
1000 - Jaladhi Soft Technology - Sat Dec 12 10:00:08 IST 2020
1000 - Jaladhi Soft Technology - Sat Dec 12 10:00:08 IST 2020