> ## 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.

# Kotlin Data Class

A data class in Kotlin is a specialized class whose primary purpose is to hold state, for which the compiler automatically generates standard utility functions based on the properties defined in the primary constructor.

```kotlin theme={"dark"}
data class User(val name: String, var age: Int)
```

When a class is marked with the `data` keyword, the Kotlin compiler automatically derives the following member functions using only the properties declared in the primary constructor:

* **`equals()`**: Evaluates structural equality, comparing the values of the properties rather than the object references in memory.
* **`hashCode()`**: Generates a hash code consistent with `equals()`. If two instances are structurally equal, they will produce the same hash code.
* **`toString()`**: Returns a string representation of the object in the format `"ClassName(prop1=value1, prop2=value2)"`.
* **`componentN()`**: Generates functions (`component1()`, `component2()`, etc.) corresponding to the properties in their order of declaration. This enables destructuring declarations.
* **`copy()`**: Creates a new instance of the object, allowing the modification of specific properties while retaining the values of the rest.

```kotlin theme={"dark"}
val user1 = User("Alice", 25)

// Utilizing the generated copy() function
val user2 = user1.copy(age = 26) 

// Utilizing the generated componentN() functions via destructuring
val (name, age) = user1 
```

## Strict Compiler Requirements

To ensure the deterministic generation of these utility functions, data classes must adhere to the following rules:

1. The primary constructor must have at least one parameter.
2. All primary constructor parameters must be explicitly marked as `val` (read-only) or `var` (mutable).
3. Data classes cannot be `abstract`, `open`, `sealed`, or `inner`.

## Class Body Properties Exclusion

The compiler strictly limits the scope of generated functions to the primary constructor. Properties declared within the class body are entirely excluded from the generated `equals()`, `hashCode()`, `toString()`, `copy()`, and `componentN()` implementations.

```kotlin theme={"dark"}
data class Person(val name: String) {
    // This property is ignored by all compiler-generated data class functions
    var age: Int = 0 
}
```

## Inheritance

Data classes can extend other classes and implement interfaces. However, if the generated methods (`equals`, `hashCode`, or `toString`) are explicitly implemented in the data class body or are `final` in a superclass, the compiler will not generate them and will use the existing implementations instead.

<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 Kotlin 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>
