How do I use val and var inside Kotlin classes?

In Kotlin classes, val and var are used to declare properties.

  • val means read-only after initialization
  • var means mutable / can be reassigned

Basic example

class User {
    val id: Int = 1
    var name: String = "Alice"
}

Usage:

fun main() {
    val user = User()

    println(user.id)      // 1
    println(user.name)    // Alice

    user.name = "Bob"     // OK: name is var

    // user.id = 2        // Error: id is val
}

val inside a class

Use val when the property should not be reassigned after it gets a value.

class Product {
    val sku: String = "ABC-123"
}

You can read it:

val product = Product()
println(product.sku)

But you cannot assign a new value:

// product.sku = "XYZ-999" // Not allowed

var inside a class

Use var when the property can change.

class Counter {
    var count: Int = 0

    fun increment() {
        count++
    }
}

Usage:

fun main() {
    val counter = Counter()

    counter.increment()
    counter.increment()

    println(counter.count) // 2
}

Declaring properties in the constructor

A common Kotlin style is to put properties directly in the class constructor.

class User(
    val id: Int,
    var name: String
)

This creates a class with:

  • a read-only id
  • a mutable name

Usage:

fun main() {
    val user = User(1, "Alice")

    println(user.id)
    println(user.name)

    user.name = "Bob" // OK

    // user.id = 2    // Error
}

Important distinction

If you write this:

val user = User(1, "Alice")

The variable user itself cannot point to another User, because it is a val.

But if the object has var properties, those properties can still change:

val user = User(1, "Alice")

user.name = "Bob" // OK, because name is var

// user = User(2, "Charlie") // Error, because user is val

So:

val user

means the reference cannot be reassigned.

var name

inside the class means the property can be changed.

Rule of thumb

Use val by default, and only use var when the value really needs to change.

class Person(
    val birthYear: Int,
    var displayName: String
)

Here, birthYear probably should not change, but displayName might.

How do I define constructors and initialize class properties in Kotlin?

In Kotlin, you usually define constructors and initialize properties directly in the class header using a primary constructor.

1. Primary constructor

The most common style is:

class Person(val name: String, var age: Int)

This defines:

  • a class named Person
  • a read-only property name
  • a mutable property age
  • a constructor that requires both values

Usage:

fun main() {
    val person = Person("Alice", 30)

    println(person.name)
    println(person.age)

    person.age = 31
    println(person.age)
}

Here, val name: String and var age: Int are both constructor parameters and class properties.


2. Constructor parameters without properties

If you omit val or var, the parameter is only available during initialization:

class Person(name: String) {
    val uppercaseName = name.uppercase()
}

Usage:

fun main() {
    val person = Person("Alice")

    println(person.uppercaseName)
}

In this example, name is not a property. You cannot access person.name unless you declare it with val or var.


3. Initialize properties in the class body

You can initialize properties using constructor values:

class Rectangle(val width: Int, val height: Int) {
    val area: Int = width * height
}

Usage:

fun main() {
    val rectangle = Rectangle(5, 4)

    println(rectangle.area)
}

Output:

20

4. Use an init block

If you need validation or setup logic, use an init block:

class User(val username: String, val age: Int) {
    init {
        require(username.isNotBlank()) {
            "Username must not be blank"
        }

        require(age >= 0) {
            "Age must not be negative"
        }
    }
}

The init block runs when an object is created:

fun main() {
    val user = User("kotlinFan", 25)

    println(user.username)
}

5. Default constructor values

You can give constructor parameters default values:

class Product(
    val name: String,
    val price: Double = 0.0,
    val inStock: Boolean = true
)

Usage:

fun main() {
    val freeSample = Product("Sticker")
    val laptop = Product("Laptop", 999.99, false)

    println(freeSample.price)
    println(laptop.inStock)
}

6. Named arguments

Named arguments make constructor calls clearer:

class Book(
    val title: String,
    val author: String,
    val pages: Int
)

fun main() {
    val book = Book(
        title = "Kotlin Basics",
        author = "JetBrains",
        pages = 250
    )

    println(book.title)
}

7. Secondary constructors

Kotlin also supports secondary constructors, but they are less common:

class Car {
    val brand: String
    val year: Int

    constructor(brand: String, year: Int) {
        this.brand = brand
        this.year = year
    }
}

Usage:

fun main() {
    val car = Car("Toyota", 2024)

    println(car.brand)
    println(car.year)
}

However, this is usually better written with a primary constructor:

class Car(val brand: String, val year: Int)

8. Primary and secondary constructors together

If a class has a primary constructor, secondary constructors must delegate to it using this(...):

class Employee(val name: String, val role: String) {
    constructor(name: String) : this(name, "Employee")
}

Usage:

fun main() {
    val employee = Employee("Sam")
    val manager = Employee("Dana", "Manager")

    println(employee.role)
    println(manager.role)
}

9. Late initialization with lateinit

For mutable non-null properties initialized later, use lateinit var:

class Session {
    lateinit var token: String

    fun start(token: String) {
        this.token = token
    }
}

Usage:

fun main() {
    val session = Session()

    session.start("abc123")

    println(session.token)
}

Use lateinit carefully. Accessing it before initialization causes an exception.


10. Custom getters and setters

You can customize property access:

class Temperature(celsius: Double) {
    var celsius: Double = celsius
        set(value) {
            require(value >= -273.15) {
                "Temperature cannot be below absolute zero"
            }
            field = value
        }

    val fahrenheit: Double
        get() = celsius * 9 / 5 + 32
}

Usage:

fun main() {
    val temperature = Temperature(25.0)

    println(temperature.fahrenheit)

    temperature.celsius = 30.0
    println(temperature.fahrenheit)
}

Quick summary

class Person(
    val name: String,
    var age: Int = 0
) {
    init {
        require(name.isNotBlank()) {
            "Name cannot be blank"
        }
    }

    val isAdult: Boolean
        get() = age >= 18
}

This example shows:

  • val name: read-only property initialized from constructor
  • var age: mutable property with a default value
  • init: validation logic
  • isAdult: computed property

In most Kotlin code, prefer a primary constructor with val or var properties unless you specifically need more complex construction logic.

How do I create a class and an object in Kotlin?

In Kotlin, you create a class with the class keyword, and you create an object instance by calling the class constructor.

class Person {
    var name: String = "Unknown"
    var age: Int = 0
}

fun main() {
    val person = Person()

    person.name = "Alice"
    person.age = 25

    println("${person.name} is ${person.age} years old")
}

Output:

Alice is 25 years old

Class with a constructor

A more common Kotlin style is to define properties directly in the constructor:

class Person(
    val name: String,
    var age: Int
)

fun main() {
    val person = Person("Alice", 25)

    println("${person.name} is ${person.age} years old")
}

Here:

  • class Person(...) defines a class.
  • val name is a read-only property.
  • var age is a mutable property.
  • Person("Alice", 25) creates an object of the class.

Kotlin object keyword

Kotlin also has the object keyword, which creates a singleton object:

object AppConfig {
    val appName = "My Kotlin App"
    val version = "1.0"
}

fun main() {
    println(AppConfig.appName)
    println(AppConfig.version)
}

Unlike a class, you do not create instances of an object. There is only one instance, and you access it directly by name.

How do I use reified types with inline functions in Kotlin?

In Kotlin, reified types are used with inline functions to enable type information to be available at runtime. Normally, type parameters in generics are erased at runtime due to type erasure, but reified allows the type to remain available for reflective operations or type-specific logic.

Here are the key points to use reified types with inline functions:

  1. Mark the function as inline: The function must be an inline function for it to use reified types.
  2. Use the reified modifier: The generic type parameter must be marked as reified to prevent type erasure and keep the type information available at runtime.
  3. Call the function without explicitly passing the type: The compiler will infer the type automatically.

Here’s an example that demonstrates using reified types:

Example: Filtering a List Based on Type

inline fun <reified T> List<Any>.filterByType(): List<T> {
    return this.filter { it is T }.map { it as T }
}

fun main() {
    val mixedList: List<Any> = listOf(1, "Hello", 2.5, "World", 42)

    // Use the reified function to filter only Strings
    val strings: List<String> = mixedList.filterByType()
    println(strings) // Output: [Hello, World]

    // Use the reified function to filter only Integers
    val integers: List<Int> = mixedList.filterByType()
    println(integers) // Output: [1, 42]
}

Explanation:

  1. Inline Functions: Inline functions replace the function body at the call site, enabling the type information to persist after type erasure.
  2. Reified Modifier: When you use reified T, you can check the type (it is T) or even fetch its class (T::class) at runtime because the type information is preserved.
  3. Flexible Filtering: In the above example, the filter function dynamically determines the type of each element and includes the matching elements in the resulting list.

When to Use Reified Types

  • When you need access to the type at runtime (e.g., to perform type checking or reflection).
  • When working with generic functions that act differently based on the type parameter.

Note:

  • You can only use reified with inline functions.
  • Avoid overusing inline functions, as they can increase code size due to function duplication at each call site.

How do I apply tail recursion in Kotlin functions for performance optimization?

In Kotlin, you can use tail recursion to optimize recursive functions by preventing stack overflow and enabling a more optimized execution during runtime. A tail-recursive function is one where the recursive call is the last operation performed in the function.

To achieve this, Kotlin provides the tailrec modifier, which instructs the compiler to optimize the recursion into an iterative loop during compilation. This eliminates the need for additional stack frames, making the function more efficient.

Here’s how you can apply tail recursion in Kotlin:

Key Considerations

  1. Recursive Call as the Last Statement
    • The recursive call must be the last executable statement in the function for the tailrec modifier to work.
  2. No Further Computation After Recursive Call
    • If there are operations that need to be performed after the recursive call, the function cannot be optimized as tail-recursive.
  3. Using the tailrec Modifier
    • Explicitly annotate the function with tailrec to enable this optimization.

Example: Factorial Function Using Tail Recursion

Here’s an example of a factorial function using tail recursion:

fun main() {
    println(factorial(5))  // Output: 120
}

tailrec fun factorial(n: Int, acc: Int = 1): Int {
    return if (n == 0) acc else factorial(n - 1, acc * n)
}

Explanation:

  • The base case is when n == 0, where the accumulated value acc is returned.
  • The recursive call factorial(n - 1, acc * n) is performed as the last operation, making the function tail-recursive.
  • The tailrec modifier ensures that this recursive function is optimized into a loop during compilation.

Example: Fibonacci Function Using Tail Recursion

Here’s another example for calculating Fibonacci numbers:

fun main() {
    println(fibonacci(10))  // Output: 55
}

tailrec fun fibonacci(n: Int, a: Int = 0, b: Int = 1): Int {
    return if (n == 0) a else fibonacci(n - 1, b, a + b)
}

Explanation:

  • The base case is when n == 0, where a (the current Fibonacci number) is returned.
  • The recursive call fibonacci(n - 1, b, a + b) is the last operation in the function.

Benefits of Using Tail Recursion

  1. Avoid Stack Overflow: Tail recursion enables Kotlin to optimize recursion into loops, avoiding stack overflow for deep recursion.
  2. Improved Performance: Optimized tail-recursive functions execute more efficiently due to their iterative nature.

Limitations

  • Tail recursion cannot be applied if the recursive call is not the last operation in your function.
  • Functions with additional computations following the recursive call must be refactored if you want to make them tail-recursive.

Keynote

Not all recursive problems are tail-call optimizable. If your problem involves maintaining state across recursive calls where calculations depend on the return value of the recursive function, using a tail-recursive approach might not be feasible. In such cases, consider using iterative approaches or data structures like stacks.