Primary vs Secondary Constructor in Kotlin: Key Differences and Usage
primary constructor is a concise way to initialize a class with parameters directly in its header, while secondary constructors are additional constructors defined inside the class body for alternative initialization. The primary constructor is preferred for simple initialization, and secondary constructors provide flexibility for multiple ways to create an object.Quick Comparison
This table summarizes the main differences between primary and secondary constructors in Kotlin.
| Aspect | Primary Constructor | Secondary Constructor |
|---|---|---|
| Definition | Declared in class header | Declared inside class body with constructor keyword |
| Number per class | At most one | Multiple allowed |
| Initialization | Directly initializes properties | Can delegate to primary constructor or other secondary constructors |
| Usage | For main initialization logic | For alternative ways to create instances |
| Syntax simplicity | More concise and clear | More verbose, used for special cases |
Access to init blocks | Automatically calls init blocks | Must delegate to primary constructor to call init blocks |
Key Differences
The primary constructor is part of the class header and is the main way to pass parameters to a class. It allows you to declare and initialize properties directly, making the code concise and easy to read. When you create an instance, the primary constructor runs first, followed by any init blocks for additional setup.
Secondary constructors are defined inside the class body using the constructor keyword. They provide alternative ways to create an object with different parameters or initialization logic. Each secondary constructor must either delegate to the primary constructor or another secondary constructor to ensure the class is properly initialized.
While the primary constructor is limited to one per class, you can have multiple secondary constructors to handle various initialization scenarios. Secondary constructors are useful when you need to support different sets of parameters or complex initialization that doesn't fit well in the primary constructor.
Code Comparison
Here is an example showing a class with a primary constructor that initializes properties directly.
class Person(val name: String, val age: Int) { init { println("Person created: $name, age $age") } } fun main() { val person = Person("Alice", 30) }
Secondary Constructor Equivalent
The same class using a secondary constructor inside the class body. It does not delegate to the primary constructor but initializes properties directly.
class Person { val name: String val age: Int constructor(name: String, age: Int) { this.name = name this.age = age println("Person created: $name, age $age") } } fun main() { val person = Person("Alice", 30) }
When to Use Which
Choose a primary constructor when your class has a straightforward initialization with a fixed set of parameters. It keeps your code clean and concise, and works well with Kotlin features like data class.
Choose secondary constructors when you need multiple ways to create an object, such as optional parameters or different initialization logic. They are helpful for backward compatibility or complex setups that don't fit neatly into the primary constructor.
Key Takeaways
init blocks; secondary constructors do so only if they delegate properly.