Immutable employee class I have created an employee object to be immutable.

package com.immutable.ex01;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public final class Employee implements Cloneable, Externalizable{
    private final Integer age;
    private final String name;
    private final List&lt;String&gt; companies;
    private final Date dob;

    public Employee(Integer age, String name,List&lt;String&gt; companies, Date dob) {
        this.age = age;
        this.name = name;
        this.companies = companies;
        this.dob = dob;
    }

    public Integer getAge() {
        return this.age;
    }

    public String getName() {
        return this.name;
    }

    public Date getDob() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(this.dob);
        return cal.getTime();
    }

    public List&lt;String&gt; getCompanies() {
        List&lt;String&gt; clone = new ArrayList&lt;String&gt;(this.companies.size());
        for(String companyName : this.companies) {
            clone.add(companyName);
        }
        return clone;
    }

    @Override
    public String toString() {
        StringBuffer strb = new StringBuffer();
        strb.append("AGE").append(" ").append(this.age)
            .append(", NAME ").append(this.name)
            .append(", DOB ").append(this.getDateString())
            .append(", COMPANIES WORKED IN ");      
        for(String companyName : this.companies) {
            strb.append(companyName).append(",");
        }

        strb.deleteCharAt(strb.length() - 1);
        return strb.toString();
    }

    @Override
    public Object clone() throws CloneNotSupportedException{
        throw new CloneNotSupportedException("Cannot be cloned");
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        throw new IOException("This operation is not supported");
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException {
        throw new IOException("This operation is not supported");
    }

    private String getDateString() {
        String date = null;
        DateFormat df = null;
        try {
            df = new SimpleDateFormat("dd MMM yyyy");
            date = df.format(this.dob);
        }catch(Exception e) {
            e.printStackTrace();
        }
        return date;
    }
}


My main method class

package com.immutable.ex01;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainTest01 {
    public static void main(String [] args) {
        Employee emp = null;
        List&lt;String&gt; companyList = null;
        Date date = null;
        try {
            companyList = new ArrayList&lt;String&gt;();
            companyList.add("IBM");
            companyList.add("Google");
            companyList.add("Norton");
            companyList.add("Seable");
            companyList.add("Nekki");
            date = toDate("28 Oct 1981");
            emp = new Employee(23, "John Molkovich", companyList, date);

            date = emp.getDob();

            date = new Date(); 

            companyList = emp.getCompanies();
            companyList.add("Toyo Corp");
            System.out.println(emp);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }


    private static Date toDate(String dateString) throws Exception{
        Date d = null;
        DateFormat df = new SimpleDateFormat("dd MMM yyyy");
        d = df.parse(dateString);
        return d;
    }
}


Please review my code and check if I have achieved immutability for my employee class.
Your Employee is frozen in time; they can never get any older!

Age should be calculated from date-of-birth and “today”, not stored in an immutable field. 



The getCompanies() method could be written simply as:

return new ArrayList&lt;&gt;(this.companies);


Or perhaps:

return Collections.unmodifiableList(this.companies);