RiaChoi Text Logo.
Data Integrity in Java - Defensive Copy

Java

DataIntegrity

DefensiveCopy

Data Integrity in Java - Defensive Copy

Part1. Data Integrity

Ria Choi • October 21st, 2026

What is Data Integrity

Bank Transfer
Data Integrity

Data integrity refers to the overall accuracy, completeness, and reliability of data throughout its entire lifecycle.


Why should we keep data integrity?
Data integrity is essential because it ensures that information remains accurate, trustworthy, and reliable for safe and effective decision-making.


About this series

In this series, I’ll be sharing my learning journey on my research to find better ways to keep data integrity in Java.


Defensive Copy

Defensive Copy is one of the ways to keep the data intact from being modified or destroyed by simply copying the original data.

 

Scenario

Gym membership management app
Your receipts

🏋️ Gym membership management app

Let’s say you need to keep your clients receipts every time.
You don’t want to modify the original data, rather it needs to be added on.

In this case, we can use newArrayList<> to make new array each time.

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

//Receipt Class
class Receipt {
    private String paymentId;
    private int amount;

    public Receipt(String paymentId, int amount) {
        this.paymentId = paymentId;
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "Payment ID: " + paymentId + ", Price: " + amount + "dollars";
    }
}

// Payment Management Class
public class PaymentManager {
    private List<Receipt> receipts;

    public PaymentManager(List<Receipt> initialReceipts) {
        this.receipts = new ArrayList<>
          
    public List<Receipt> getReceipts() {
        return new ArrayList<>(receipts);
    }

    // Add New Payment
    public void addReceipt(Receipt receipt) {
        this.receipts.add(receipt);
    }
}


Defensive Copy in Java VS Object.freeze()

If you’re a JavaScript programmer, you would soon be able to link this with Object.freeze(). While both of them is used for the same purpose - to maintain data integrity and prevent unintended side effects by shielding internal data from unauthorized external modification, it works differently.

// 표 넣기


Example code from my recent project

While working on the project Floww - an AI agent mandate purpose wallet, I’ve encountered the same solution to keep the data integrity.


TaskModel.java

TaskModel.java
Line 25~28

This code here is to keep the allowed recipients separated when the mandate happens.
* AllowedRecipients: It has fields like merchantId, recipientAddress.

 

Share

Related contents