How to Create Immutable Class in Java

Create Immutable Class in Java

In this tutorial, We will show you about, How to Create Immutable Class in Java. There are two possible ways are attached in the post.

#1 Creating without Setters method

package com.dineshkrish;

public class Customer {

	private String customerId;
	private String customerName;

	public Customer(String customerId, String customerName) {

		this.customerId = customerId;
		this.customerName = customerName;

	public String getCustomerId() {
		return customerId;

	public String getCustomerName() {
		return customerName;


#2 Making Attributes as final

package com.dineshkrish;

public class Employee {

	private final int empId;
	private final String empName;

	public Employee(int empId, String empName) {

		this.empId = empId;
		this.empName = empName;

	public int getEmpId() {
		return empId;

	public String getEmpName() {
		return empName;