Immutable class (immutable) means that the field of the instance is immutable after the instance is created for the mine. 8 Java-supplied wrapper classes and Java.lang.String classes are immutable classes
If you need to create your own custom immutable classes
(1) Use private and final to modify the field of the class
(2) provides a constructor with parameters to initialize field in class based on incoming parameters
(3) Provide Getter method only for field of this class, do not provide setter method for field of class
(4) If necessary, rewrite the Equals method of object and the Hashcode method
/*
I defined a immutable class.
*/
public class Address
{
The two members of the immutable class are string classes and are immutable classes, that is, the first condition that the address satisfies the immutable class
Private final String detail;
Private final String postcode;
Public Address ()
{
}
Provides a constructor with parameters
Public address (String detail, string postcode)
{
This.detail = detail;
This.postcode = postcode;
}
Getter method provided, no setter method provided
Public String Getdetail ()
{
return this.detail;
}
Public String Getpostcode ()
{
return This.postcode ();
}
The Equals () method and the Hashcode () method of the object class have been overridden
public boolean equals (Object obj)
{
if (obj = this)
{
return true;
}
if (obj!= null && address.class = = Obj.getclass ())
{
Address-AD = (address) obj;
if (this.getaddress () = = Ad.getaddress () && this.getpostcode () = = Obj.getpostcode ())
{
return true;
}
}
return false;
}
}
public boolean hashcode ()
{
Return Detail.hashcode () +postcode.hashcode () * 31;
}
When the inner inside is a reference to a normal class, this generic class can change the field of a class at will, and when using the final modifier to refer to a variable, only that the reference type variable is not assignable, but the object to which the reference type variable is still changeable, this creates a problem: When immutable classes are created, If the type of field it contains is mutable, the field of its object is still mutable---the immutable class is actually a failure
Class Name
{
Private String FirstName;
Private String LastName;
Public Name ()
{
}
Public Name (String firstName, String lastName)
{
This.firstname = FirstName;
This.lastname = LastName;
}
Public String Getfirstname ()
{
return this.firstname;
}
Public String Getlastname ()
{
return this.lastname;
}
public void Setfirstname (String firstName)
{
This.firstname = FirstName;
}
public void Setlastname (String lastName)
{
This.lastname = LastName;
}
}
public class Person
{
private final name name;
Public person (name name)
{
THIS.name = name;
}
Public Name GetName ()
{
return this.name;
}
public static void Main (string[] args)
{
Name n = new name ("Chen", "odd");
Person p = new person (n);
System.out.println (P.getname (). Getfirstname ());
N.setfirstname ("Chen");
System.out.println (P.getname (). Getfirstname ());
}
}
It is not hard to find that the fiestname of the name of the person object has changed
In order to preserve the immutable class of the person object, you must protect the reference type Field:name of the person object, make the program inaccessible to the person object's name field, and cannot use the variability of the name field to alter the person object
Public person (name name)
{
this.name = new name (Name.getfirstname (), Name.getlastname ());
}
Public Name GetName ()
{
return new Name (Name.getfirsname (), Name.getlastname ());
}
Copies the instance of the name object to the name Field passed into the person object so that even changing name does not change the name instance of the person object
Immutable classes of cached instances
Immutable class of instance state can not be changed, it is convenient to chant multiple object sharing, if the program needs to use the same immutable class instance, you should consider caching the immutable class instances, after all, repeat the creation of the same object does not have much meaning
Class Cacheimmutable
{
private static int max_size = 10;
private static cacheimmutable[] cache = new Cacheimmutable[max_size];
private static int pos = 0;
Private final String name;
Private cacheimmutable (String name)
{
THIS.name = name;
}
Public String GetName ()
{
return name;
}
public static cacheimmutable valueof (String name)
{
for (int i=0; i<max_size; ++i)
{
if (Cache[i]!= null && cache[i].getname (). Equals (name))
{
return cache[i];
}
}
if (pos = = max_size)
{
Cache[0] = new cacheimmutable (name);
pos = 1;
}
Else
{
cache[pos++] = new cacheimmutable (name);
}
return cache[pos-1];
}
public boolean equals (Object obj)
{
if (this = obj)
{
return true;
}
if (obj!= null && cacheimmutable.class = = Obj.getclass ())
{
Cacheimmutable ci = (cacheimmutable) obj;
Return Name.equals (Ci.getname ());
}
return false;
}
public int hashcode ()
{
return Name.hashcode ();
}
}
public class Cacheimmutabletest
{
public static void Main (string[] args)
{
Cacheimmutable C1 = cacheimmutable.valueof ("Hello");
cacheimmutable C2 = cacheimmutable.valueof ("Hello");
SYSTEM.OUT.PRINTLN (C1 = = C2);
}
}
The Java.lang.Integer class also provides caching mechanisms that if you use the new constructor to create an object, you return a new integer object each time, and if you use the valueof () method to create an integer object, the object created by the method is cached
Static final integer[] cache = new integer[-(-128) + 127 + 1];
static {
for (int i=0; i<cache.length; i++)
{
Cache[i] = new Integer (i-128);
}
}