What is a primary constructor in Kotlin?

Technology CommunityCategory: KotlinWhat is a primary constructor in Kotlin?
VietMX Staff asked 4 years ago

The primary constructor is part of the class header. Unlike Java, you don’t need to declare a constructor in the body of the class. Here’s an example:

class Person(val firstName: String, var age: Int) {
    // class body
}

The main idea is by removing the constructor keyword, our code gets simplified and easy to understand.