> ## 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 Primary Constructor

A primary constructor in Kotlin is the main entry point for class instantiation, defined directly within the class header. It immediately follows the class name and optional type parameters, serving to declare constructor parameters and, optionally, class-level properties in a single declaration.

By default, the `constructor` keyword can be omitted unless the constructor requires visibility modifiers or annotations.

```kotlin theme={"dark"}
// Explicit constructor keyword
class User constructor(username: String) { /*...*/ }

// Implicit constructor keyword (standard practice)
class User(username: String) { /*...*/ }
```

## Parameter vs. Property Declaration

The primary constructor differentiates between standard parameters and class properties based on the presence of `val` or `var` keywords.

* **`val`**: Declares a read-only property. A backing field is generated, along with a getter.
* **`var`**: Declares a mutable property. A backing field is generated, along with a getter and a setter.
* **No keyword**: Declares a standard constructor parameter. It does not become a class property and is only accessible within property initializers and `init` blocks.

```kotlin theme={"dark"}
class Configuration(
    val host: String,      // Read-only property
    var port: Int,         // Mutable property
    timeout: Int           // Constructor parameter (not a property)
) {
    // 'timeout' is accessible here during initialization
    val connectionTimeout = timeout * 1000 
}
```

## Initialization Blocks

The primary constructor cannot contain executable code. Any setup logic or validation must be placed within one or more `init` blocks.

During instantiation, `init` blocks and property initializers are executed in the exact order they appear in the class body. Primary constructor parameters are fully accessible within these blocks.

```kotlin theme={"dark"}
class NetworkClient(val endpoint: String) {
    val isSecure: Boolean = endpoint.startsWith("https")

    init {
        require(endpoint.isNotBlank()) { "Endpoint cannot be blank" }
    }

    init {
        // Executes after the first init block
        println("Client initialized for $endpoint")
    }
}
```

## Modifiers and Annotations

If the primary constructor requires an annotation (e.g., `@Inject`) or a visibility modifier (e.g., `private`, `internal`), the `constructor` keyword becomes mandatory. The modifiers are placed immediately before the `constructor` keyword.

```kotlin theme={"dark"}
class DatabaseHelper @Inject private constructor(
    val connectionString: String
) {
    /*...*/
}
```

## Default Arguments

Primary constructor parameters can define default values. If all parameters have default values, the Kotlin compiler automatically generates an additional parameterless (no-arg) constructor, which is required by certain reflection-based frameworks (like Jackson or JPA).

```kotlin theme={"dark"}
class Session(
    val id: String = UUID.randomUUID().toString(),
    val duration: Int = 3600
)
```

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