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
Related Questions on Kotlin Program
A. A new version of Java.
B. A JavaScript framework.
C. A statically-typed programming language for the JVM, Android, and browser.
D. A database management system.
Which platform does Kotlin primarily target?
A. Python Bytecode
B. JavaScript
C. JVM (Java Virtual Machine) Bytecode
D. PHP

Join The Discussion