Design Converter
Education
Last updated on Jan 2, 2025
Last updated on Jan 2, 2025
Software Development Executive - III
If you are working with Kotlin, chances are you have encountered the error: "kotlin primary constructor call expected."
This issue typically arises when Kotlin's primary constructor rules aren't adhered to, particularly in cases involving class declaration, secondary constructors, or class hierarchy.
In this blog, we will break down this error and explain how you can resolve it effectively.
Kotlin classes have a primary constructor that is defined in the class header. When you create a secondary constructor, Kotlin requires that it must either delegate to the primary constructor or explicitly call a constructor from the parent class (if present). Failing to do so results in the "primary constructor call expected"
error.
This error can be confusing if you're not fully familiar with how Kotlin constructors work, especially when dealing with class properties and initialization code. Let’s start with some fundamental concepts.
In Kotlin, constructors are special member functions used to initialize a class. Kotlin supports two types of constructors:
Primary Constructor: Declared in the class header, the primary constructor initializes class properties and runs any initializer blocks.
Secondary Constructors: Declared inside the class body, these provide additional ways to initialize the class. They must delegate to the primary constructor if it exists.
The primary constructor is declared directly after the class name in the class header. If your class doesn’t have a primary constructor, define one using the constructor
keyword, or move your initialization code inside a secondary constructor.
1class Person(val name: String, val age: Int) { 2 // Primary constructor initializes name and age 3}
Here, the primary constructor parameters (name
and age
) directly initialize the class properties.
When using secondary constructors, you must delegate them to the primary constructor by using the this()
keyword. This ensures that all initialization code defined in the primary constructor is executed.
1class Person(val name: String, val age: Int) { 2 constructor(name: String) : this(name, 0) { 3 println("Secondary constructor called with name only") 4 } 5}
In this Person
class, the secondary constructor delegates to the primary constructor and supplies a default value (0 for age
).
If your class inherits from a parent class, and the parent class has a primary constructor, your child class must explicitly call it. Use the super
keyword to ensure proper delegation.
1open class Employee(val id: Int) 2 3class Manager(id: Int, val teamSize: Int) : Employee(id) { 4 // Manager calls the primary constructor of Employee 5}
If your primary constructor doesn’t fully handle the initialization, use an initializer block (init
). These are executed immediately after the primary constructor call.
1class Student(val name: String, val age: Int) { 2 init { 3 println("Initialized Student: $name, $age years old") 4 } 5}
You can also use multiple init
blocks for structured initialization.
Ensure that your secondary constructors don’t create circular delegation (e.g., constructor A calls constructor B, which calls constructor A again). This violates Kotlin's constrained syntax for constructors.
1open class Person(val name: String) 2 3class Student : Person { 4 constructor(name: String, age: Int) // Error: Primary constructor call expected 5}
Solution: Add a call to the parent class constructor.
1class Student(name: String, val age: Int) : Person(name)
1class Person { 2 val name: String 3 4 constructor(name: String) // Error: Primary constructor call expected 5}
Solution: Add a primary constructor or delegate to an existing constructor.
1class Person(val name: String) { 2 constructor(name: String, age: Int) : this(name) { 3 println("Age: $age") 4 } 5}
Using default values simplifies your code and avoids creating multiple constructors unnecessarily.
1class Employee(val name: String = "Unknown", val age: Int = 0)
Here, you can create class instances without explicitly passing arguments:
1val emp = Employee() 2println(emp.name) // Output: Unknown
The "Kotlin primary constructor call expected"
error often boils down to understanding how constructors work in Kotlin. Whether you're dealing with primary constructors, secondary constructors, or class inheritance, following Kotlin's rules ensures smooth execution. By defining proper class properties, leveraging default values, and using initializer blocks, you can effectively avoid this error and write clean, concise Kotlin code. Keep practicing with examples like the Person
class and the Student
class to master Kotlin's constructor mechanics!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.