0
votes

Suppose I have a String type lazy val :

    Class LazyVals { 

     lazy val message = "I am lazy on + " + System.currentTimeMillis().toString    

     def changeLazy = { message + " Not!!" }
    }

Will changeLazy change the "evaluate once only" nature of message?

1

1 Answers

3
votes

Code to calculate message will be evaluated once only. You can't change this behavior.

currentTimeMillis will be called only once. Concatenation message + " Not!!" will be performed on each changeLazy call with the same result, but can be optimized by jvm.