,

Saturday 1 July 2017

Immutable class in java 2 (Reference type)

If a class has a reference type variable , the content of it can be modified after creation of object.
It is necessary to take precaution and add proper code to avoid modification of reference type after creation of object.
In the below class , we have used reference type variable qualification of type ArrayList.

package com.immutable2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author ganeshrashinker
 *
 */
// make the class final
public final class Employee {
// make the variables private and final
private final String name;
private final int age;
private final List<String> qualification;

// define the getters for the private variables
public String getName() {
return name;
}

public int getAge() {
return age;
}

public List<String> getQualification() {
// return qualification;
return qualification;
}

public Employee(String name, int age, List<String> qualification) {
super();
this.name = name;
this.age = age;
// Below line will allow modifying elements in list
// this.qualification = qualification;
                // Below line will not allow modifying elements in list
this.qualification = Collections.unmodifiableList(qualification);

}

@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", qualification=" + qualification + "]";
}


}

Collections.unmodifiableList(qualification) will not allow the user to modify the content of the qualification list.

package com.immutable2;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author ganeshrashinker
 *
 */
public class MainClass {

public static void main(String[] args) {
List<String> qualification = new ArrayList<>();
qualification.add("BA");
qualification.add("MA");
Employee emp = new Employee("John", 35, qualification);
//emp.getQualification().set(0, "DA");
System.out.println(emp);

}


}

Note: It is necessary to take precaution of reference type variables(objects) of the class and put additional restriction so that the content of the reference type variables cannot be modified.

Saturday 24 June 2017

Immutable class in java


Immutable class is one whose properties cannot be modified.
In other words, the state of objects of immutable class does not change after it is initialised.

To make a class immutable follow below mentioned steps.
1) Make the class final so that it cannot be extended.
2) Declare the variables as private and final.
3) Declare and define the getters for all variable and do not create the setters.
4) Create constructor with parameters.

Below is the example of Immutable class

package com.immutable;
/**
 * 
 * @author ganeshrashinker
 *
 */
//make the class final
public final class Employee {
//make the variables private and final
private final String name;
private final int age;
//define the getters for the private variables
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Employee(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + "]";
}

Class with main method

package com.immutable;
/**
 * 
 * @author ganeshrashinker
 *
 */

public class MainClass {

public static void main(String[] args) {
Employee employee = new Employee("Rahul", 55);
System.out.println(employee);
}

}

Output is: Employee [name=Rahul, age=55]

Note: Immutable classes are beneficial for Caching purpose and they are thread safe.