Semantics require that List("1", "1")
is evaluated once every time method
is called, just in case the call has side-effects.
AFAIK there is no modifier that would allow you to change that behavior. If you really, really do not want to declare list
in the enclosing class, you could do:
class X {
object MethodHolder {
val list = List("1", "1")
def method(param: ???) = ...
}
import MethodHolder.method
// rest of class
}
Note: You are not allowed to use the final
keyword for function variables. In Scala, final
does only prevent overriding (see answer to this post).