Having a Scala class with a companion object
object BB {
val time = System.currentTimeMillis()
}
class BB {
val time = System.currentTimeMillis()
}
I was wondering if the object was working just like in Java where If I remember the first thing to be evaluated in the class was the statics and then finally the constructor.
But here when I create a new instance of BB and check the times, the object it´s not evaluated first.
object main extends App {
val time = System.currentTimeMillis()
private val bb = new BB
println(s"App:$time ")
println("Instance" + bb.time )
println(s"Static ${BB.time }")
}
So then I've been wondering how the initialization in Scala works?
Regards.
BB, then yes, that will be initialized whenBB.timeis invoked (unless the field is defined aslazy valordef).