Skip to main content

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.

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.
// 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.
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.
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.
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).
class Session(
    val id: String = UUID.randomUUID().toString(),
    val duration: Int = 3600
)
Master Kotlin with Deep Grasping Methodology!Learn More