> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Record

A Java `record` is a restricted, implicitly `final` class designed to act as a transparent carrier for immutable data. Introduced as a standard feature in Java 16, it eliminates boilerplate by automatically generating the standard members required for data-centric classes based entirely on its state description (the record header).

When you declare a record, you define its components in the header:

```java theme={"dark"}
public record Point(int x, int y) { }
```

Based on this declaration, the Java compiler automatically generates the following bytecode equivalents:

1. **State:** A `private final` field for each component defined in the header (`x` and `y`).
2. **Constructor:** A canonical constructor whose signature exactly matches the record components, assigning each argument to its corresponding field.
3. **Accessors:** Public read accessor methods named exactly after the components (e.g., `x()` and `y()`, rather than `getX()` and `getY()`).
4. **Object Methods:** Implementations of `equals()`, `hashCode()`, and `toString()` that operate on all components defined in the header.

## Constructor Customization

Records support a specialized **compact constructor**. This constructor omits the parameter list and is primarily used for validation or normalization before the canonical constructor implicitly assigns the parameters to the fields.

```java theme={"dark"}
public record User(String username, int age) {
    
    // Compact constructor
    public User {
        if (age < 0) {
            throw new IllegalArgumentException("Age cannot be negative");
        }
        if (username != null) {
            username = username.trim(); // Normalization
        }
        // The compiler implicitly inserts:
        // this.username = username;
        // this.age = age;
    }
}
```

You can also declare overloaded constructors, provided they ultimately delegate to the canonical constructor using `this(...)`.

```java theme={"dark"}
public record User(String username, int age) {
    // Overloaded constructor delegating to the canonical constructor
    public User(String username) {
        this(username, 0); 
    }
}
```

## Technical Constraints and Characteristics

Because records are designed with strict semantics regarding immutability and state transparency, they are subject to specific language constraints:

* **Inheritance:** All records implicitly extend `java.lang.Record`. Because Java does not support multiple class inheritance, a record cannot `extend` any other class.
* **Subclassing:** Records are implicitly `final`. They cannot be declared `abstract`, and no class can extend a record.
* **Interfaces:** Records can implement multiple interfaces. The generated methods or custom methods within the record body fulfill the interface contracts.
* **Instance Fields:** A record cannot declare any instance fields outside of the components defined in the record header.
* **Static Members:** Records can declare `static` fields, `static` initializers, and `static` methods.
* **Instance Methods:** You can declare custom instance methods within the record body, or override the implicitly generated methods (like `toString()` or the accessors), provided the overrides do not compromise the immutable semantics of the record.
* **Serialization:** Records serialize and deserialize differently than standard classes. They bypass `writeObject`, `readObject`, and default serialization mechanisms, relying exclusively on the canonical constructor to reconstruct the object, making them inherently safer against deserialization attacks.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
