Here is the example, how to create immutable Class in java
package com.nitesh;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
* Make all fields final and private.
* Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is
* to make the constructor private and construct instances in factory methods.
* If the instance fields include references to mutable objects, don't allow those objects to be changed:
* Don't provide methods that modify the mutable objects.
* Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies,
* and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.
* @author Nitesh.Gupta
*
*/
public final class MyImmuatable {
/**
* Final Integer field, Integer is anyways immutable
*/
private final Integer immutableField1;
/**
* Final String field
*/
private final String immutableField2;
/**
* Final Date object which is mutable and need to be taken care explicitly
*/
private final Date immutableField3;
/**
* Final List Object which is mutable
*/
private final List immutableField4;
/**
* Constructor which is taking all inputs through parameters
* @param immutableField1
* @param immutableField2
* @param immutableField3
* @param immutableField4
*/
public MyImmuatable (Integer immutableField1, String immutableField2, Date immutableField3,List immutableField4){
this.immutableField1 = immutableField1;
this.immutableField2 = immutableField2;
this.immutableField3 = new Date(immutableField3.getTime());
this.immutableField4 = Collections.unmodifiableList(immutableField4);
}
public Integer getImmutableField1() {
return immutableField1;
}
public String getImmutableField2() {
return immutableField2;
}
public Date getImmutableField3() {
return immutableField3;
}
public List getImmutableField4() {
return immutableField4;
}
@Override
public String toString() {
return "MyImmuatable [immutableField1=" + immutableField1 + ", immutableField2=" + immutableField2
+ ", immutableField3=" + immutableField3 + ", immutableField4=" + immutableField4 + "]";
}
}
package com.nitesh;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* Don't provide "setter" methods — methods that modify fields or objects referred to by fields.
* Make all fields final and private.
* Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is
* to make the constructor private and construct instances in factory methods.
* If the instance fields include references to mutable objects, don't allow those objects to be changed:
* Don't provide methods that modify the mutable objects.
* Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies,
* and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.
* @author Nitesh.Gupta
*
*/
public final class MyImmuatable {
/**
* Final Integer field, Integer is anyways immutable
*/
private final Integer immutableField1;
/**
* Final String field
*/
private final String immutableField2;
/**
* Final Date object which is mutable and need to be taken care explicitly
*/
private final Date immutableField3;
/**
* Final List Object which is mutable
*/
private final List
/**
* Constructor which is taking all inputs through parameters
* @param immutableField1
* @param immutableField2
* @param immutableField3
* @param immutableField4
*/
public MyImmuatable (Integer immutableField1, String immutableField2, Date immutableField3,List
this.immutableField1 = immutableField1;
this.immutableField2 = immutableField2;
this.immutableField3 = new Date(immutableField3.getTime());
this.immutableField4 = Collections.unmodifiableList(immutableField4);
}
public Integer getImmutableField1() {
return immutableField1;
}
public String getImmutableField2() {
return immutableField2;
}
public Date getImmutableField3() {
return immutableField3;
}
public List
return immutableField4;
}
@Override
public String toString() {
return "MyImmuatable [immutableField1=" + immutableField1 + ", immutableField2=" + immutableField2
+ ", immutableField3=" + immutableField3 + ", immutableField4=" + immutableField4 + "]";
}
}
No comments:
Post a Comment