Browse by Topic

Popular

Latest

Latest Java Posts

Diagram showing a Java record implementing an interface while being final, with composition alternatives.
Java

Java Record Inheritance: What Works and What Doesn't

java record inheritance: Learn how Java records handle inheritance: why they are final, how they implement interfaces, and when to use sealed hierarchies or composition.

Java recordsinheritancesealed interfacescomposition
A Java record symbol connected to an interface symbol, representing a data carrier implementing a contract.
Java

Java Record Implements Interface: Syntax and Use Cases

Learn how Java records can implement interfaces, how generated accessors map to interface methods, and when to use a record instead of a class.

Java recordsinterfacesdata modelingimmutability
Illustration of a Java record with immutable fields and a lock symbol representing enforced immutability.
Java

Java Record Immutability: Guarantees and Limitations

Java records guarantee immutability for their own fields, but mutable components need extra care. Learn what the compiler enforces, where records are only shallowly immutable, and how to handle validation, defensive copying, and serialization.

Java recordsimmutabilitydata classesJava 17
A visual comparison between a Java record and a Java class, showing a concise record declaration next to a more verbose class definition.
Java

Java Record vs Class: When to Use Each

Java records are concise, shallowly immutable data carriers with component-based equality; classes support mutable state, custom equality, and inheritance. This guide explains the practical differences and when each is the better fit.

Java recordsJava classesimmutabilitydata modeling
An illustration showing a Java record with its generated toString output displayed as a formatted string, representing the automatic string representation of record components.
Java

Java Record toString: Default Format and Customization

A practical look at Java record toString(): the default output format, how to override it, and how to handle sensitive fields and array components.

Java RecordstoStringData ClassesObject Methods
Diagram showing a Java record with components feeding into a hash function to produce a hashCode value.
Java

Java Record hashCode: Default and Custom

Learn how Java records implement hashCode by default, when overriding it makes sense, and how to write a custom hashCode for record classes.

Java recordshashCodeequalsObject methods
Diagram showing two Java record objects with matching component values being compared by equals()
Java

Java Record equals: How It Works and When to Override

Understand how Java records generate equals() and hashCode() automatically, when to override them, and common pitfalls.

Java recordsequalshashCodeobject equality
Diagram showing a Java record component mapped to its generated accessor method with a defensive copy shield.
Java

Java Record Accessor: Defaults and Customization

java record accessor: Explains how Java record accessor methods are generated, when to customize them, and how defensive copies protect mutable components.

Java RecordsAccessor MethodsDefensive CopyCompact Constructor
Diagram showing a Java record with its automatically generated methods and custom methods added by a developer.
Java

Java Record Methods: Custom Methods and Overrides

Learn how Java record methods work: auto-generated accessors, custom instance methods, compact constructors, static methods, and interface implementations.

javarecordsmethodsaccessors
Diagram showing Java record components mapping to private final fields and accessor methods.
Java

Java Record Fields: Syntax and Behavior

Understand Java record fields: how they are declared, accessed, validated, and restricted, with practical examples and edge cases.

Java RecordsImmutabilityData ClassesJava 16