Kotlin Error – Primary Constructor call expected

Kotlin Compilation Error: Primary Constructor call expected occurs when a class has a kotlin primary constructor, but a kotlin secondary constructor does not delegate to it.

In Kotlin, if a primary constructor is present, every secondary constructor must call the primary constructor directly or indirectly. This is done with the this keyword in the secondary constructor header.

This compilation error could be resolved by including a call to the primary constructor, or previous secondary constructors that make a call to the primary constructor, using “this” keyword.

Why Kotlin shows Primary Constructor call expected

The error appears because Kotlin must know how the primary constructor parameters and initialization logic will be executed. The primary constructor is responsible for initializing properties declared in the class header. If a secondary constructor skips it, Kotlin cannot safely initialize the object.

The basic constructor delegation syntax is shown below.

</>
Copy
class ClassName(primaryValue: Type) {
    constructor(otherValue: Type) : this(primaryValue) {
        // secondary constructor body
    }
}

The : this(…) part is not optional when the class has a primary constructor and the secondary constructor is not delegating to another secondary constructor.

Example that recreates Kotlin Primary Constructor call expected error

Let us see an example below which recreates Kotlin Primary Constructor call expected – Compile Error

example.kt

</>
Copy
/**
 * Demo program for handling Kotlin Compilation Error Primary Constructor call expected
 */

fun main(args: Array) {    
    val myObj = Student("Arjun", 15)
    myObj.printMsg()
}

class Student(var name: String="") {
    var age: Int = 14
    constructor (name: String, age: Int){
        this.age = age
    }
    fun printMsg(){
        println("Name is $name. Age is $age.");
    }
}

Output

example.kt
Error:(12, 4) Primary constructor call expected
Warning:(12, 17) Parameter 'name' is never used

In the above program, Student has a primary constructor with the parameter name. The secondary constructor accepts name and age, but it does not call this(name). Because of that, Kotlin reports the primary constructor call expected error.

How to handle Kotlin Primary Constructor call expected – Compile Error

Let us include the call to primary constructor or previous secondary constructors that make a call to the primary constructor. The corrected program is

example.kt

</>
Copy
fun main(args: Array) {    
    val myObj = Student("Arjun", 15)
    myObj.printMsg()
}

class Student(var name: String="") {
    var age: Int = 14
    constructor (name: String, age: Int): this(name){
        this.age = age
    }
    fun printMsg(){
        println("Name is $name. Age is $age.");
    }
}

Output

Name is Arjun. Age is 15.

The corrected secondary constructor uses : this(name). This calls the primary constructor and initializes the name property before the body of the secondary constructor assigns the age value.

Kotlin secondary constructor delegation rule

The important rule is simple: when a Kotlin class has a primary constructor, each secondary constructor must ultimately delegate to that primary constructor.

Constructor situationRequired Kotlin constructor call
Secondary constructor directly calls the primary constructorUse : this(primaryConstructorArguments)
Secondary constructor calls another secondary constructorUse : this(otherSecondaryConstructorArguments), and that chain must finally call the primary constructor
Class has no primary constructorA secondary constructor does not need to call this(…) unless it delegates to another constructor
Class extends a superclassConstructor delegation may also involve super(…), depending on the superclass constructor

Constructor delegation happens before the body of the secondary constructor is executed. So values needed by the primary constructor must be passed in the this(…) call, not assigned later inside the secondary constructor body.

Fixing Primary Constructor call expected with constructor chaining

A secondary constructor can also call another secondary constructor. This is called constructor chaining. However, the chain must still reach the primary constructor.

</>
Copy
fun main() {
    val student = Student("Arjun", 15, "A")
    student.printMsg()
}

class Student(var name: String) {
    var age: Int = 0
    var section: String = "Not assigned"

    constructor(name: String, age: Int) : this(name) {
        this.age = age
    }

    constructor(name: String, age: Int, section: String) : this(name, age) {
        this.section = section
    }

    fun printMsg() {
        println("Name is $name. Age is $age. Section is $section.")
    }
}

Output

Name is Arjun. Age is 15. Section is A.

Here, the third constructor calls the second constructor using : this(name, age). The second constructor calls the primary constructor using : this(name). Therefore, the constructor chain is valid.

Fixing the error by moving properties to the primary constructor

In many cases, the simplest solution is to avoid a secondary constructor and declare all required properties in the primary constructor. This is often cleaner when the secondary constructor only assigns values to properties.

</>
Copy
fun main() {
    val student = Student("Arjun", 15)
    student.printMsg()
}

class Student(var name: String = "", var age: Int = 14) {
    fun printMsg() {
        println("Name is $name. Age is $age.")
    }
}

Output

Name is Arjun. Age is 15.

This version does not need a secondary constructor. The primary constructor accepts both name and age, and default values are provided for both properties.

Primary constructor call expected with init block

If a class has an init block, the primary constructor and the init block are executed before the body of the secondary constructor. This is another reason why Kotlin requires a secondary constructor to delegate properly.

</>
Copy
fun main() {
    val student = Student("Arjun", 15)
}

class Student(var name: String) {
    var age: Int = 0

    init {
        println("Primary constructor initialized name as $name")
    }

    constructor(name: String, age: Int) : this(name) {
        this.age = age
        println("Secondary constructor initialized age as $age")
    }
}

Output

Primary constructor initialized name as Arjun
Secondary constructor initialized age as 15

The output shows that the primary constructor path runs first, and then the secondary constructor body runs. If the secondary constructor does not delegate using : this(name), this initialization order cannot be guaranteed.

When Kotlin secondary constructor does not need this call

If the class does not declare a primary constructor, a secondary constructor can initialize the object without calling this(…). In the following example, the class header has no constructor parameters.

</>
Copy
fun main() {
    val student = Student("Arjun", 15)
    student.printMsg()
}

class Student {
    var name: String = ""
    var age: Int = 0

    constructor(name: String, age: Int) {
        this.name = name
        this.age = age
    }

    fun printMsg() {
        println("Name is $name. Age is $age.")
    }
}

Output

Name is Arjun. Age is 15.

This is valid because there is no primary constructor to call. However, for most data-holding classes in Kotlin, a primary constructor is usually clearer and shorter.

Common mistakes that cause Kotlin constructor errors

  • Adding a secondary constructor to a class that already has a primary constructor, but forgetting : this(…).
  • Passing values only inside the constructor body instead of passing required values to the primary constructor.
  • Using a secondary constructor when default values in the primary constructor would be enough.
  • Creating multiple secondary constructors where one constructor in the chain does not finally reach the primary constructor.
  • Declaring a constructor parameter such as name but not using it to initialize the corresponding primary constructor property.

Kotlin Primary Constructor call expected FAQ

What is Primary Constructor call expected in Kotlin?

It is a Kotlin compilation error that appears when a class has a primary constructor and a secondary constructor does not call it directly or indirectly using this(…).

How do I call a primary constructor from a secondary constructor in Kotlin?

Use : this(…) after the secondary constructor parameter list. For example, constructor(name: String, age: Int) : this(name) calls the primary constructor with the name value.

Can one secondary constructor call another secondary constructor in Kotlin?

Yes. A secondary constructor can call another secondary constructor using : this(…). The constructor chain must eventually call the primary constructor if the class has one.

Is a secondary constructor required in Kotlin?

No. Many Kotlin classes can be written with only a primary constructor. Use a secondary constructor only when you need an alternative way to create the object and default parameter values are not enough.

Does the init block run before a Kotlin secondary constructor body?

Yes. The primary constructor path and init blocks run before the body of the secondary constructor. That is why the secondary constructor must delegate to the primary constructor when one exists.

Editorial QA checklist for this Kotlin constructor error tutorial

  • Confirm that the article explains the exact cause of Primary Constructor call expected: missing delegation from a secondary constructor.
  • Confirm that the fix uses : this(…) in the secondary constructor header, not inside the constructor body.
  • Confirm that the failing and corrected Kotlin examples use the same Student class so the change is easy to compare.
  • Confirm that constructor chaining explains how one secondary constructor can indirectly reach the primary constructor.
  • Confirm that the tutorial distinguishes classes with a primary constructor from classes that only have secondary constructors.

Conclusion

In this Kotlin Tutorial, we have learned how to handle the Kotlin Primary Constructor call expected – Compile Error by including the call to primary constructor using “this” keyword. Learn about Kotlin Secondary Constructor.