I tried using abstract valin a tag to initialize another value. I have received NullPointerException. I threw the behavior back to the minimum test case:
trait MessagePrinter {
val message: String
println(message)
}
class HelloPrinter extends MessagePrinter {
val message = "Hello World"
}
val obj = new HelloPrinter()
println(obj.message)
This small program gives the following result:
null
Hello World
I got the impression that the shaft will never change. Is this the expected behavior or is it a compiler error? How can I get around this problem and print Hello Worldduring initialization?
source
share