CodesJava

Easy learning with example program codes

How to write Immutable class in java


All wrapper classes and String class are immutable classes.

You can write your own immutable class, when creating immutable class just keep in mind that after creating an object of this class object can’t be modified. Any change in existing object result into new object.

  1. Make final class so that it cannot inherit.
  2. Object state is made up of its properties, declare all properties final. So that its properties value will remain constant.
  3. Object properties value can be set using setter methods, so only define getter methods for all properties.

Example:

ImmutableClassExample.java

/**
 * This program is used to create a immutable class.
 * @author codesjava
 */
final class Student{
	//declare all properties final.
	final String rollNo;	
 
	public Student(String rollNo){
		this.rollNo = rollNo;
	}
 
	//only create getter method.
	public String getRollNo() {
		return rollNo;
	}
}
 
public class ImmutableClassExample {
	public static void main(String args[]){
		//creating Student object. 
		Student obj = new Student("MCA/07/06");
 
		System.out.println(obj.getRollNo());
	}
}

Output:

MCA/07/06

Download this example.

Advantages/Benefits of immutable class.

  1. Objects are thread safe by default.
  2. No need to synchronize immutable objects explicitly.

Disadvantages of immutable classes.

As discussed any change in immutable object result into a new object, hence result in unnecessary garbage.
 
Next Topic: toString() method in java with example.
Previous Topic: Substring in java with example.

Related Topics:

String handling in java with example.
String Comparison in java with example.
String concatenation in java with example.
Substring in java with example.
How to write Immutable class in java with example.
toString() method in java with example.
Commonly used methods of String Class with example.
charAt(int index) String function in java with example.
startsWith(String prefix) and endsWith(String suffix) String functions in java.
indexOf(String str) and lastIndexOf(String str) String functions in java.
toLowerCase() and toUpperCase() String functions in java.
intern() String functions in java with example.
length() String functions in java with example.
trim() String functions in java with example.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap