← Back to Kotlin Course | Chapter 6: Classes & Objects | Lesson 2 of 7

Constructors

Constructors are the special setup instructions a class runs whenever a brand-new object of that kind gets built.
Syntax
kotlin
class ClassName(val property: Type, param: Type) {   // primary constructor
    constructor(other: Type) : this(other, defaultValue) {
        // secondary constructor
    }
}

The Primary Constructor

The primary constructor is part of the class header itself, listing the parameters needed to build an instance directly after the class name.

Example: The Primary Constructor

markup
// Define a class `Book`
class Book(val title: String, val author: String)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `book`, set to `Book("Kotlin in Action", "Dmitry Jemerov")`
    val book = Book("Kotlin in Action", "Dmitry Jemerov")
    // Print "${book.title} by ${book.author}" to the console, with a trailing newline
    println("${book.title} by ${book.author}")
}

Secondary Constructors

A constructor keyword inside the class body defines an alternate way to build an instance; every secondary constructor must eventually delegate to the primary constructor using : this(...).

Example: Secondary Constructors

markup
// Define a class `Book`
class Book(val title: String, val author: String) {
    constructor(title: String) : this(title, "Unknown")
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `book`, set to `Book("Kotlin Basics")`
    val book = Book("Kotlin Basics")
    // Print "${book.title} by ${book.author}" to the console, with a trailing newline
    println("${book.title} by ${book.author}")
}

Constructor Parameters vs Properties

A parameter listed without val/var in the primary constructor is only available inside the constructor and any init blocks -- to expose it as a member, it must be declared as val or var, or explicitly assigned to a property.

Example: Constructor Parameters vs Properties

markup
// Define a class `Circle`
class Circle(radius: Double) {
    // Declare a read-only variable `area` (a Double), set to `Math.PI * radius * radius`
    val area: Double = Math.PI * radius * radius
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `circle`, set to `Circle(2.0)`
    val circle = Circle(2.0)
    // Print "Area: ${circle.area}" to the console, with a trailing newline
    println("Area: ${circle.area}")
}

Default Values in Constructors

Just like regular functions, constructor parameters can have default values, letting some arguments be omitted when creating an instance.

Example: Default Values in Constructors

markup
// Define a class `User`
class User(val name: String, val role: String = "member")

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `admin`, set to `User("Ana", "admin")`
    val admin = User("Ana", "admin")
    // Declare a read-only variable `user`, set to `User("Bo")`
    val user = User("Bo")
    // Print "${admin.name}: ${admin.role}" to the console, with a trailing newline
    println("${admin.name}: ${admin.role}")
    // Print "${user.name}: ${user.role}" to the console, with a trailing newline
    println("${user.name}: ${user.role}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that a secondary constructor must delegate to the primary constructor with : this(...) when a primary constructor exists.
  2. Duplicating validation logic across multiple secondary constructors instead of putting shared setup in the primary constructor or an init block.
  3. Confusing constructor parameters with properties; a plain parameter (no val/var) is not accessible outside the constructor unless stored.
Chapter Summary
  • The primary constructor is declared in the class header, right after the class name.
  • Secondary constructors are declared with the constructor keyword inside the class body and must delegate to the primary constructor.
  • Constructor parameters without val/var are only usable inside the constructor/init block, not as class members.
  • Multiple secondary constructors let a class be created in different ways while sharing common setup logic.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.