skip to content

What is a Kotlin primary constructor, and how can it declare class properties directly in the class header?

level: juniorimportance: must knowfreq 80%

answer

  1. Header after class name = primary constructor
  2. val/var prefix → auto property
  3. No val/var → plain param, not a member
  4. constructor keyword only for modifiers/annotations
  5. No body; logic goes in init blocks

basics

~10 s

The primary constructor is written in the class header. If you put val or var before a parameter, Kotlin creates a property with that name automatically, so you don't write extra assignment code.

solid answer

~40 s

Kotlin's primary constructor lives in the class header right after the class name: `class User(val name: String, var age: Int)`. Parameters prefixed with `val` or `var` are not just constructor arguments — the compiler turns each into a class property (read-only for `val`, mutable for `var`) and generates the field assignment for you. Parameters without `val`/`var` are plain constructor parameters, usable only inside `init` blocks and property initializers, not as members. The `constructor` keyword is normally omitted and only required when you add visibility modifiers or annotations, e.g. `class C private constructor(...)`. The primary constructor itself contains no code block; initialization logic goes in `init { }` blocks.

code

kotlin · 6 lines
kotlin
class User(val name: String, var age: Int = 0)

val u = User("Ada")
println(u.name)   // Ada  (val property)
u.age = 36        // var property is mutable
// u.name = "x"   // compile error: val cannot be reassigned

go deeper

for a junior

Knows val/var in the header create properties and that the primary constructor sits after the class name.

for a middle

Distinguishes plain params from property params and knows where each is usable.

for a senior

Explains generated getters/setters, when constructor keyword is required, and default-argument interplay.

for a principal

Frames primary-constructor choices for API design: immutability defaults, encapsulation, and binary-compatibility implications of exposed properties.

## What the primary constructor is In Kotlin a class can declare a **primary constructor** as part of the class header, immediately after the class name: ```kotlin class User(val name: String, var age: Int) ``` There is no separate constructor body with curly braces here — the primary constructor cannot contain statements directly. ## val/var parameters become properties A constructor parameter prefixed with `val` or `var` does two jobs at once: - it is a **constructor parameter** (you pass a value when creating the object), and - it declares a **property** (a member field with a generated getter, plus a setter for `var`). - `val name: String` → read-only property `name` (getter only). - `var age: Int` → mutable property `age` (getter + setter). A parameter **without** `val`/`var` is an ordinary parameter, not a member: ```kotlin class User(name: String) { // name is NOT a property val displayName = name.uppercase() // usable in initializers/init only } ``` ## The constructor keyword Usually you omit the `constructor` keyword. It becomes required when the constructor carries **visibility modifiers** or **annotations**: ```kotlin class Token private constructor(val value: String) class Bean @Inject constructor(val dep: Dep) ``` ## Default arguments Parameters may have default values, removing the need for overloads: ```kotlin class Server(val host: String = "localhost", val port: Int = 8080) Server() // localhost:8080 Server(port = 9090) // named argument skips host ``` ## Where logic goes Because the primary constructor has no body, run-time setup lives in `init { }` blocks and property initializers, executed top to bottom in declaration order.

  • What is the difference between a parameter with val and one without it?
    With `val`/`var` it becomes a class property accessible everywhere; without it, it is only a constructor parameter visible to initializers and `init` blocks, not a member.
  • When must you write the constructor keyword explicitly?
    When the primary constructor has a visibility modifier (e.g. `private`) or an annotation (e.g. `@Inject`).

Like filling out a form where ticking a box also files the answer permanently in your record (val/var), versus just reading it once and discarding it (plain param).

saying these in an interview costs you the question

  • Claiming every constructor parameter automatically becomes a property
  • Saying the primary constructor can contain executable statements directly
  • Confusing val (read-only) with var (mutable)
  • Thinking the constructor keyword is always mandatory
  • Believing a plain parameter is accessible from regular member functions

context