Close Menu
Şevket Ayaksız
    What's Hot

    Put One Landscape Section Inside a Portrait Word Document

    November 18, 2026

    Oracle INSTR: Treat a Missing Match as a Separate Result

    November 18, 2026

    Java Records: Final Fields Do Not Freeze Every Object Inside Them

    November 18, 2026
    • software
    • Gadgets
    Şevket AyaksızŞevket Ayaksız
    • Home
    • Technology

      Apple: iPhone 17 and Older Models Hit With Unexpected $100 Price Increase

      September 10, 2026

      Apple Watch Intelligence Could Be a Major Accessibility Breakthrough

      September 10, 2026

      Apple May Skip the Base iPhone 18 This Year

      September 10, 2026

      FCC changes robot vacuum rules, potentially affecting future models

      August 8, 2026

      Samsung’s new 2TB 990 SSD drops to its lowest price yet

      August 6, 2026
    • Adobe

      InDesign Packaging: Prepare a Project Another Designer Can Actually Open

      November 18, 2026

      Illustrator Clipping Masks: Edit the Window and the Artwork Separately

      November 13, 2026

      InDesign Text Threads: Keep One Story Flowing Across Your Layout

      November 13, 2026

      Premiere Markers: Keep Source Notes Separate from Edit Decisions

      November 12, 2026

      Photoshop Smart Filters: Keep Effects Adjustable Throughout an Edit

      November 11, 2026
    • Microsoft

      Put One Landscape Section Inside a Portrait Word Document

      November 18, 2026

      Excel Freeze Panes: Keep the Labels That Make Your Data Understandable

      November 18, 2026

      Excel Paste Values: When a Report Needs a Snapshot

      November 13, 2026

      Word Table Headers: Keep Long Tables Understandable on Every Page

      November 12, 2026

      Excel Structured References: Write Formulas That Explain Their Data

      November 11, 2026
    • java

      Java Records: Final Fields Do Not Freeze Every Object Inside Them

      November 18, 2026

      Java Stream Matching: Handle Empty Collections Deliberately

      November 13, 2026

      Java floorDiv and floorMod: Handle Negative Coordinates Deliberately

      November 12, 2026

      Java Period and Duration: Define What “One Day Later” Means

      November 11, 2026

      Java Comparator Null Handling: Decide Where Missing Values Belong

      November 11, 2026
    • Oracle

      Oracle INSTR: Treat a Missing Match as a Separate Result

      November 18, 2026

      Oracle Bind Variables: Keep Data Separate from SQL Structure

      November 13, 2026

      Oracle SAVEPOINT: Undo Part of a Transaction Without Losing Its Earlier Work

      November 12, 2026

      Oracle SUBSTR and SUBSTRB: Decide Whether Your Limit Means Characters or Bytes

      November 12, 2026

      Oracle LEFT JOIN Filters: Preserve the Rows Your Report Needs

      November 11, 2026
    Şevket Ayaksız
    Anasayfa » Java Records: Final Fields Do Not Freeze Every Object Inside Them
    java

    Java Records: Final Fields Do Not Freeze Every Object Inside Them

    By sevketayaksizNovember 18, 2026No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Java Records: Final Fields Do Not Freeze Every Object Inside Them
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Java records provide a concise way to represent related values. A small declaration can replace much of the repetitive code normally needed for a data-carrying class.

    That convenience can hide an important design question: what happens when a record contains a mutable object? A record’s components cannot simply be reassigned after construction, but that does not automatically make every referenced object immutable.

    Understand the shallow guarantee

    Oracle describes records as shallowly immutable data carriers. Each component corresponds to a private final field and a public accessor. The compiler can also supply the canonical constructor and implementations of equals, hashCode, and toString.

    Consider this declaration:

    record ReadingList(String name, List<String> titles) {}

    The record holds a reference to a list. If the supplied list remains mutable and another part of the application changes it, the record can expose a different collection of titles later.

    The declaration is compact, but the ownership policy is still a developer decision.

    Establish ownership during construction

    For a record intended to represent a stable reading list, a compact constructor can capture an unmodifiable list:

    import java.util.List;
    
    record ReadingList(String name, List<String> titles) {
        ReadingList {
            titles = List.copyOf(titles);
        }
    }

    List.copyOf produces an unmodifiable list containing the supplied elements. Later structural changes to the original collection are not reflected in the returned list. It also rejects a null collection or null elements.

    In this example, the elements are strings. That choice makes the intended value model relatively simple.

    Document the null policy instead of allowing callers to discover it accidentally. An empty reading list and a missing list may have different meanings in the application.

    Examine the elements as well

    Replacing List<String> with List<MutableBook> changes the analysis. Preventing additions and removals does not prevent the book objects themselves from changing.

    Ask what the record is meant to capture. Is it a historical snapshot of titles at a specific moment, or a view of current book objects that can be updated elsewhere?

    For a snapshot, consider storing immutable values representing the information required by the consumer. For a live view, make that behavior clear in the type’s name and documentation.

    Avoid calling a structure a snapshot when changes elsewhere can still alter what it represents.

    Test the boundaries that matter

    A useful test begins by constructing the record from a mutable source list. Change the source list afterward and check whether the record still represents the original collection.

    Next, examine the accessor’s result. If the contract promises an unmodifiable collection, attempts to change its structure should not silently succeed.

    Finally, test any mutable element types separately. This last step catches a common oversight: the collection boundary is protected, but the objects inside remain shared.

    These tests should verify the intended contract rather than merely repeat the constructor’s implementation.

    Choose records for clear value models

    Records work particularly well when their components communicate a coherent set of values. They do not remove the need to decide who owns mutable data.

    Before adopting a record, write one sentence describing whether its state should remain stable after construction. Then ensure that the component types, constructor, accessors, and tests support that promise. Concise syntax is most useful when the underlying contract is equally clear.

    Sources:

    • Oracle: Record API
    • Oracle: List.copyOf documentation
    Views: 6
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    sevketayaksiz
    • Website

    Related Posts

    Java Stream Matching: Handle Empty Collections Deliberately

    November 13, 2026

    Java floorDiv and floorMod: Handle Negative Coordinates Deliberately

    November 12, 2026

    Java Period and Duration: Define What “One Day Later” Means

    November 11, 2026
    Add A Comment

    Comments are closed.

    Şevket Ayaksız
    Instagram
    • Home
    • Adobe
    • microsoft
    • java
    • Oracle
    • Contact
    © 2026 Theme Designed by Şevket Ayaksız.

    Type above and press Enter to search. Press Esc to cancel.