Constructors and Properties
Learn how Kotlin constructors and properties make class creation shorter and cleaner.
Kotlin Code
Study the example and understand each part.
class Product(
val name: String,
var price: Double
) {
fun display() {
println("Product: $name")
println("Price: $price")
}
}
fun main() {
val product =
Product("Laptop", 55000.0)
product.display()
}
Kotlin is commonly developed using Android Studio or IntelliJ IDEA. These examples can be used as a foundation for Android application development.
Key Concept
Kotlin's primary constructor allows properties to be declared directly in the class header.
Expected Output / Result
Product: Laptop Price: 55000.0
Finished this lesson?
Mark it complete to update your course progress.