Steps to
creation of Immutable class:-
To create immutable class in java, you have to do following steps.
1. Declare the class as final so it can’t be extended.
2. Make all fields private so that direct access is
not allowed.
3. Don’t provide setter methods for variables
4. Make all mutable fields final so that it’s value can be assigned only
once.
5. Initialize all the fields via a constructor
performing deep copy.
6. Perform cloning of objects in the getter methods to
return a copy rather than returning the actual object reference.
=======================================================================
How
to make our class is immutable class?
//ImmutableClassExample.java
// ImmuatableMain.java
//ImmutableClassExample.java
package govind;
import
java.util.Date;
public final class
ImmutableClassExample {
private final
Integer immutableField1;
private final
String immutableField2;
private final
Date mutableField1;
public ImmutableClassExample(Integer immutableField1, String immutableField2,Date
date) {
super();
this.immutableField1
= immutableField1;
this.immutableField2
= immutableField2;
this.mutableField1=new Date(date.getTime());//(line----1)always create new //instance for mutable objects
//this.mutableField1=date;// (line----2)
}
public Integer getImmutableField1() {
return immutableField1;
}
public String getImmutableField2() {
return immutableField2;
}
public Date getMutableField1() {
return new
Date( mutableField1.getTime());//(line----3)always return new instance //for mutable
objects
//return
mutableField1;//(line----4)
}
}
// ImmuatableMain.java
package govind;
import
java.util.Date;
public
class ImmuatableMain {
public
static void
main(String[] args) {
Date dt=new
Date();
ImmutableClassExample ice=new
ImmutableClassExample(10, "govind",
dt);
System.out.println(ice.getImmutableField1()+" "+ice.getImmutableField2()+" "+ice.getMutableField1());
Date d = ice.getMutableField1();
System.out.println("Change the date and print date");
d.setDate(2-06-2011);
System.out.println(d);
System.out.println("plz
After the changing the date value check
date value is changed or not ");
System.out.println(ice.getImmutableField1()+" "+ice.getImmutableField2()+" "+ice.getMutableField1());
}
}
o/p:-
view:----------------------
case:1:- (if
line-1 and line 3 is not commented:----------and line 2 and line 4 is commented
) then o/p:-------
10 govind Thu May 03 02:10:33 IST 2018
Change the date and print date
Tue Oct 23 02:10:33 IST 2012
plz After the changing the date value check date value it is
changed or not
10 govind Thu May 03 02:10:33 IST 2018
Case:-2(if line-2 and line 4 is not commented:-------and line-1
and line-3 is commented) then o/p:---
10 govind Thu May 03 02:23:44 IST 2018
Change the date and print date
Tue Oct 23 02:23:44 IST 2012
plz After the changing the date value check date value is changed or not
10 govind Tue Oct 23 02:23:44 IST 2012
Description:
--------------
Immutable Class
In java
1.
Make java class
as final
2.
Make all
instance variable declared as final
3.
No setter method
for instance variable.
4.
Integer class is
immutable class. Therefore we provide only getters.
It does not have setters method
5.
String class is
immutable class. Therefore we provide only getters. It does not have setters
method
6.
Problem comes
when instance variable is mutable class. we have to take special care let’s
take a Date class as instance variable Date is a Mutable class
7.
this.mutableField1=date;//don’t
do this Date class is mutable class, performing
above step can make our class change its state through date reference variable
here Date class has setters to change its state.
8.
return
mutableField1 don’t do this as discussed above it will return a Date to caller method
where client can change its state.
No comments:
Post a Comment