Primary constructors are an important part of the Kotlin programming language, allowing developers to initialize an object's properties when it is created. In Kotlin, primary constructors are declared as part of the class definition, and they can take parameters that are used to set the initial values of the object's properties.
To declare a primary constructor, the constructor keyword is used, followed by the parameter list. The constructor parameters are declared in the same way as regular function parameters, and they can have default values. For example, the following class defines a primary constructor that takes two parameters, name and age:
class Person(val name: String, val age: Int)
When creating an instance of this class, the constructor parameters must be specified. For example, the following code creates a new Person object with the name "John Doe" and age 30:
val person = Person("John Doe", 30)
Primary constructors are an important part of the Kotlin programming language, allowing developers to initialize an object's properties when it is created. In Kotlin, primary constructors are declared as part of the class definition, and they can take parameters that are used to set the initial values of the object's properties.
To declare a primary constructor, the constructor keyword is used, followed by the parameter list. The constructor parameters are declared in the same way as regular function parameters, and they can have default values. For example, the following class defines a primary constructor that takes two parameters, name and age:
class Person(val name: String, val age: Int)
When creating an instance of this class, the constructor parameters must be specified. For example, the following code creates a new Person object with the name "John Doe" and age 30:
val person = Person("John Doe", 30)
Primary constructors can also be used to initialize properties that are not declared in the class. To do this, the init keyword is used, followed by the property assignment. For example, the following class defines a primary constructor that initializes the name property and uses the init block to initialize the age property:
class Person(val name: String) { init { age = 30 } }
There are several advantages to using primary constructors in Kotlin:
Primary constructors should be used in the following situations:
Primary constructors should not be used in the following situations:
Primary constructors are a powerful tool that can be used to improve the readability, reduce the boilerplate code, and increase the flexibility of Kotlin code. However, it is important to understand the advantages and disadvantages of using primary constructors before deciding whether or not to use them in a particular situation.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.