I want a class that I can create instances of with one variable unset (the id
), then initialise this variable later, and have it immutable after initial
Google's Guava library (which I recommend very highly) comes with a class that solves this problem very well: SettableFuture. This provides the set-once semantics that you ask about, but also a lot more:
setException
method);Future
family of types in general used for synchronization between threads in multithreaded programs, so SettableFuture
plays very nicely with these.Java 8 also has its own version of this: CompletableFuture.