skip to content

Primary Constructor

The primary constructor lives in the class header, and marking its parameters val or var declares properties in the same line. Default arguments on it are what let Kotlin avoid the telescoping-constructor pattern entirely.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

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

open as a page

How do default arguments in a Kotlin primary constructor work, and how do they interact with the JVM and named arguments?

level: middleimportance: must knowfreq 70%

basics

~10 s

You can give constructor parameters default values. Callers can leave those out, and Kotlin fills in the defaults. Named arguments let you skip earlier parameters while still setting later ones.

open as a page

When is the constructor keyword required in a Kotlin primary constructor, and what does a private primary constructor enable?

level: middleimportance: should knowfreq 55%

basics

~10 s

Usually you skip the constructor keyword. You must write it when you add a visibility modifier like private, or an annotation. A private primary constructor stops outside code from calling it directly.

open as a page

Trace the initialization order when a primary constructor with val/var parameters runs alongside property initializers and init blocks. What pitfalls arise?

level: seniorimportance: should knowfreq 45%

basics

~20 s

First the constructor parameters get their values. Then property initializers and init blocks run from top to bottom in the order they appear. Using a property before it is set in that order can give you a null or wrong value.

open as a page

Explain the practical and performance differences between declaring a primary-constructor parameter as a property (val/var) versus a plain parameter.

level: seniorimportance: should knowfreq 50%

basics

~20 s

A val/var parameter is stored as a field for the object's whole life and is readable later. A plain parameter is only available while the object is being built, so it is not kept around.

open as a page