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 class in Kotlin is a user-defined blueprint for creating objects, declared using the class keyword. It encapsulates state and behavior through properties and member functions. Structurally, a Kotlin class consists of a class name, an optional class header (containing type parameters and the primary constructor), and an optional class body enclosed in curly braces. If a class has no body, the curly braces can be omitted entirely.
class EmptyClass

Constructors and Initialization

Kotlin distinguishes between a primary constructor and one or more secondary constructors. Primary Constructor The primary constructor is part of the class header. It is declared immediately after the class name and any type parameters. If the primary constructor does not have annotations or visibility modifiers, the constructor keyword can be omitted. Declaring properties (val or var) directly in the primary constructor automatically initializes them as class members.
class Person(val firstName: String, var age: Int)
The primary constructor cannot contain arbitrary execution code. Initialization logic must be placed within initializer blocks, denoted by the init keyword. During instance initialization, init blocks are executed in the exact order they appear in the class body, interleaved with property initializers.
class Person(name: String) {
    val customerKey = name.uppercase()

    init {
        require(name.isNotBlank()) { "Name cannot be blank" }
    }
}
Secondary Constructors Secondary constructors are declared within the class body using the constructor keyword. If a class has a primary constructor, every secondary constructor must delegate to the primary constructor, either directly or indirectly through another secondary constructor, using the this keyword.
class Person(val name: String) {
    var children: MutableList<Person> = mutableListOf()

    // Secondary constructor delegating to the primary constructor
    constructor(name: String, parent: Person) : this(name) {
        parent.children.add(this)
    }
}

Properties and Backing Fields

Properties in Kotlin classes are declared as mutable using var or read-only using val. Kotlin automatically generates getters for all properties, and setters for var properties. If a property requires a custom accessor that references the property’s stored value, Kotlin provides an implicit backing field accessible via the field identifier. The backing field is only generated if the property uses the default implementation of at least one accessor, or if a custom accessor references it through the field identifier.
class Rectangle(val width: Int, val height: Int) {
    // Custom getter, no backing field generated
    val area: Int
        get() = width * height 

    var color: String = "White"
        set(value) {
            // 'field' refers to the generated backing field
            field = value.trim() 
        }
}

Inheritance and Modifiers

By default, all classes in Kotlin are final at the bytecode level, meaning they cannot be subclassed. To permit inheritance, a class must be explicitly marked with the open modifier. All Kotlin classes implicitly inherit from the Any root class, which provides default implementations for equals(), hashCode(), and toString().
open class Base(val p: Int)

class Derived(p: Int) : Base(p)

Instantiation

Kotlin does not utilize the new keyword. To instantiate a class, the constructor is invoked using standard function invocation syntax.
val person = Person("Alice", 30)
val derived = Derived(42)
Master Kotlin with Deep Grasping Methodology!Learn More