I’ve been using scala’s lazy val idiom a lot and I would like to achieve something similar in Java. My main problem is that to construct some value I need some other value which is not known at object construction time, but I do not want to be able to change it afterwards. The reason for that is, I’m using a GUI library which instanciates the object on my behalf and calls a different method when everything I need is created, which is when I know the values I need.
Here are the properties I try to achieve:
* Immutability of my variable.
* Initialization in some other method than the constructor.
I do not think this is possible in Java, for only final achieves immutability of the variable and final variables cannot be initialized outside of the constructor.
What would be the closest thing in Java to what I am trying to achieve ?
One way to do it would be to push the actual instantiation of the value in question into another class. This will be final, but won’t be actually created until the class is loaded, which is deferred until it is needed. Something like the following:
This will lazily initialise the
Fooas and when desired.If you absolutely need the
Footo be an instance variable of your top-level class – I can’t think of any way off-hand to do this. The variable must be populated in the constructor, as you noted.In fact I’m not sure exactly how Scala gets around this, but my guess would be that it sets the
lazy valvariable to some kind of thunk which is replaced by the actual object when first evaluated. Scala can of course do this by subverting the normal access modifiers in this case, but I don’t think you can transparently do this in Java. You could declare the field to be e.g. aFuture<Foo>which creates the value on first invocation and caches it from that point on, but that’s not referentially transparent, and by the definition offinalI don’t see a way around this.