Why does not this code snippet compile?
class Cat (name: String) {
fun greet() { println("Hello ${this.name}") }
}
fun main() {
val thunderCat = Cat("ThunderCat")
thunderCat.greet()
}
class Cat (name: String) {
fun greet() { println("Hello ${this.name}") }
}
fun main() {
val thunderCat = Cat("ThunderCat")
thunderCat.greet()
}A. Because name is a class parameter, not a property-it is unresolved main().
B. In order to create an instance of a class, you need the keyword new
C. The reference to name needs to be scoped to the class, so it should be this.name
D. Classes cannot be immutable. You need to change var to val
Answer: Option A

Join The Discussion