Reputation: 3244
Here is my short Code :
class InnoString(abc: String)extends App{
val message=abc
override def toString(): String ={
return message
}
def concat(sub : String): String={
return s"$sub$message"
}
}
object checkInno extends App{
val x = new InnoString("harshil")
println(x)
println(x.concat("there "))
}
The result now is :
here null there null
Expected output is :
here harshil there harshil
Upvotes: 0
Views: 38
Reputation: 12783
This happens because App
changes how variables are initialized. Remove the extends App
and it will behave as you expect.
Upvotes: 1