Primary Constructor and Property Parameters
Declare classes with primary constructors and inline property declarations.
The Primary Constructor
A Kotlin class declares its primary constructor right after the class name in parentheses. No method body — just a parameter list.
Simplest Class
The primary constructor without modifiers is invisible after the class name.
class Greeter(val name: String) {
fun greet() = "Hello, $name!"
}
fun main() {
println(Greeter("Ada").greet())
}All lessons in this course
- Primary Constructor and Property Parameters
- init Blocks and Initialization Order
- Custom Getters and Setters with field
- lateinit and Lazy Initialization