Skip to content

SapnaS01/JavaImmutability

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

β˜• Java Immutability Example

This repository demonstrates how to create an immutable class in Java using a Person class as an example.


πŸ”’ What is Immutability?

Immutability means that once an object is created, its state cannot be changed. Instead of modifying an existing object, you create a new one with the updated data. Immutable objects are widely used in Java and functional programming for many reasons:

✨ Benefits of Immutable Classes:

  • βœ… Thread-safe: No synchronization needed as data can’t change.
  • βœ… Safe for caching & sharing: Can be reused freely without side effects.
  • βœ… Reliable and predictable: No accidental data changes after construction.
  • βœ… Easy to debug: Object state remains consistent and traceable.
  • βœ… Used in collections: Ideal for keys in hash-based collections like HashMap.

Example of Predefined Immutable class in java

  • String Class
  • Wrapper Classes (Integer,Long, Double, Float, Short, Byte,Boolean, Character)
  • LocalDate,LocalDateTime,LocalTime (Part of Java8 Date-Time Api)
  • BigDecimal, BigInteger

🧱 How to Create an Immutable Class in Java

To create an immutable class, follow these rules:

  1. Declare the class as final so it can't be subclassed.
  2. Make all fields private and final.
  3. Don’t provide setters for any field.
  4. Initialize all fields through the constructor only.
  5. For mutable fields (like List, Map, Date), return defensive copies.

πŸ“¦ Project Structure

src/ └── com/self/immutablity/ β”œβ”€β”€ Person.java // Immutable class └── TestImmutableClass.java // Test class to demonstrate immutability

βœ… Key Features in This Example

  • Person class is marked final (can't be subclassed)
  • Fields are private and final
  • No setters
  • Uses defensive copying for mutable fields like List
  • All fields initialized only in the constructor

πŸ›‘οΈ Defensive Copy

A defensive copy is used to protect internal state:

this.hobbies = new ArrayList<>(hobbies); // In constructor
return new ArrayList<>(hobbies);         // In getter

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages