The immutable class is the name of the class that cannot be re-assigned after it is instantiated, and the eight wrapper classes and java.lang.String provided by Java are immutable classes.
Rules to be followed for creating custom immutable classes:
1. Use private and final to modify member variables.
2. Provides a method for constructing a parameter to initialize member variables.
3. Do not provide setter methods for member variables.
4. If there is a mutable class in the member variable, you need to override the Hashcode method in object and the Equals method.
For example, create an immutable person class
Public classPerson {Private FinalString Name; Private FinalString Gender; /** Non-parametric construction method*/ PublicPerson () { This. Name= ""; This. gender= ""; } /** Parametric construction method*/ PublicPerson (string Name, String gender) { This. Name =Name; This. Gender =gender; } PublicString GetName () {returnName; } PublicString Getgender () {returngender; } /** Override Hashcode Method * (non-javadoc) * @see Java.lang.object#hashcode ()*/@Override Public inthashcode () {returnName.hashcode () +Gender.hashcode (); } /** Override Equals Method * (non-javadoc) * @see java.lang.object#equals (java.lang.Object)*/@Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj! =NULL&& Obj.getclass () = = This. GetClass ()) {Person PE=(person) obj; if( This. GetName (). Equals (Pe.getname ()) && This. Getgender (). Equals (Pe.getgender ()))return true; } return false; }
The member variables in this person class are immutable classes, and if there are mutable classes, it is problematic to create immutable classes using the above method, although the property of person is immutable, but the object referenced by the property is mutable.
This can cause the immutability of the person to be compromised, such as the following example
First create a mutable class college
Public classCollege {PrivateString Collno; PrivateString Collname; PublicCollege (String Collno, String collname) {Super(); This. Collno =Collno; This. Collname =Collname; } PublicCollege () {Super(); } PublicString Getcollno () {returnCollno; } Public voidSetcollno (String collno) { This. Collno =Collno; } PublicString Getcollname () {returnCollname; } Public voidsetcollname (String collname) { This. Collname =Collname; } @Override PublicString toString () {return"College [collno=" + Collno + ", collname=" + Collname + "]"; }
Reference College in person
Public classPerson {Private FinalCollege College; PublicPerson () { This. College =NULL; } PublicPerson (College College) {Super(); This. College =College; } PublicCollege Getcollege () {returnCollege; } @Override PublicString toString () {return"Person [college=" + College + "]"; } Public Static voidMain (string[] args) {College coll=NewCollege ("123456", "Civil Engineering"); Person PE=NewPerson (coll); System.out.println ("----->" +PE); Coll.setcollname ("Information Engineering"); This will change the person object System.out.println ("======>" +PE); }
So how can you create immutable classes with mutable properties? We just have to make the program inaccessible to the College property.
Public classPerson {Private FinalCollege College; PublicPerson () { This. College =NULL; } PublicPerson (College College) {//Create a college that has the same properties as the passed-in object, assigned to the member variable This. College =NewCollege (College.getcollno (), College.getcollname ()); } PublicCollege Getcollege () {//creates a college assigns the value of the property to it and returns return NewCollege ( This. College.getcollno (), This. College.getcollno ()); } @Override PublicString toString () {return"Person [college=" + College + "]"; }
The idea is to separate the relationships between the variable attributes in the outside world and the person class, and the program cannot directly act on properties, creating immutable classes with mutable class properties.
Immutable classes in Java