Why is a field of an object initialised every time the field is used? My expectation was that an instance of an Scala Object, in my example an instance of P, is created once since it belongs to a Scala Object. Running the code below it look like this is not the case. An instance of P is created every time a method on P is called. I could not find any explanation for this behaviour. Can anybody help? Thanks.
object LoadingTest {
println("create Object")
def p = new P()
def main(args: Array[String]) {
p.printIt()
p.printIt()
}
}
class P(){
println("create P")
def printIt()={
println("print it")
}
}
Output:
create Object
create P
print it
create P
print it