liveBook · Manning

Initializer

As explained in Kotlin in Action, 2E

A constructor is a Kotlin class member that initializes a new instance and receives constructor arguments.

A Kotlin constructor initializes a new instance and receives the arguments needed to create it. The primary constructor usually provides the concise path: its parameters can initialize properties directly, avoiding repetitive field assignments and getters. section 4.2.1 section 2.2

A primary constructor with a property

class User( val nickname: String, val isSubscribed: Boolean = true ) fun main() { val alice = User("Alice") val bob = User("Bob", false) val carol = User("Carol", isSubscribed = false) } `val` promotes a constructor parameter to a property. Default and named arguments make common construction concise while preserving explicit alternatives. section 4.2.1

Use an initializer block when construction requires executable logic that the primary-constructor syntax cannot contain. Initialization code runs when the instance is created, and a class may contain several initializer blocks. section 4.2.1

Choose the constructor form that keeps initialization explicit and maintainable:

  • Write `constructor` explicitly when the primary constructor has annotations or visibility modifiers; otherwise, omit the keyword. section 4.2.1
  • Use a secondary constructor inside the class body when several construction paths are required. section 4.2
  • Pass constructor arguments to a superclass, as in `class SocialUser(nickname: String) : User(nickname)`. section 4.2.1
  • Restrict construction with a private constructor when instances should not be created freely. section 4.2.1

Initialization versus computed access

class SocialUser(val accountId: Int) : User { override val nickname = getNameFromSocialNetwork(accountId) } The social-network lookup runs during initialization and its result is stored in a backing field. This differs from a custom getter, which recalculates a value whenever the property is accessed. section 4.2.3

class User(val nickname: String)

See the smallest idiomatic form: a primary-constructor parameter declared directly as a property. section 4.2.1

How Kotlin in Action, 2E covers it

How deep it goes
Explains primary and secondary constructors, initializer blocks, defaults, named arguments, inheritance, visibility, and backing fields.
Examples
Uses User construction, Java-to-Kotlin Person conversion, superclass delegation, restricted constructors, and social-network initialization.
What it assumes
Assumes basic classes and properties while introducing Kotlin constructor syntax and contrasting it with Java field and getter boilerplate.
What you can do after
Build it from scratch with concise primary constructors, executable initializer blocks, alternate constructors, defaults, named arguments, and constructor delegation.
How it connects
Connects constructors with properties, initializer blocks, secondary constructors, inheritance, interfaces, and backing fields.

Also explained in

Compare how 9 books explain initializer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.