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 initialisation. Effectively, I'd like a final
variable that I can initialise outside of the constructor.
Currently, I'm improvising this with a setter that throws an Exception
as follows:
public class Example {
private long id = 0;
// Constructors and other variables and methods deleted for clarity
public long getId() {
return id;
}
public void setId(long id) throws Exception {
if ( this.id == 0 ) {
this.id = id;
} else {
throw new Exception("Can't change id once set");
}
}
}
Is this a good way of going about what I'm trying to do? I feel like I should be able to set something as immutable after it's initialised, or that there is a pattern I can use to make this more elegant.
try have an int checker like
//u can try this:
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);ListenableFuture
interface).Future
family of types in general used for synchronization between threads in multithreaded programs, soSettableFuture
plays very nicely with these.Java 8 also has its own version of this:
CompletableFuture
.The "set only once" requirement feels a bit arbitrary. I'm fairly certain what you're looking for is a class that transitions permanently from uninitialized to initialized state. After all, it may be convenient to set an object's id more than once (via code reuse or whatever), as long as the id is not allowed to change after the object is "built".
One fairly reasonable pattern is to keep track of this "built" state in a separate field:
Usage:
With this pattern, you construct the object, set its values (as many times as is convenient), and then call
build()
to "immutify" it.There are several advantages to this pattern over your initial approach:
0
is just as valid an id as any otherlong
value.build()
is called, they work. Afterbuild()
is called, they throw, regardless of what values you pass. (Note the use of unchecked exceptions for convenience).final
, otherwise a developer could extend your class and override the setters.But this approach has a fairly big drawback: developers using this class can't know, at compile time, if a particular object has been initialized or not. Sure, you could add an
isBuilt()
method so developers can check, at runtime, if the object is initialized, but it would be so much more convenient to know this information at compile time. For that, you could use the builder pattern:Usage:
This is much better for several reasons:
final
fields, so both the compiler and developers know these values cannot be changed.Yes, it's a bit more complicated to maintain, but IMHO the benefits outweigh the cost.
Here's the solution I came up with based on mixing some of the answers and comments above, particularly one from @KatjaChristiansen on using assert.
At the end of the day, I suspect that my need for this is an indication of poor design decisions elsewhere, and I should rather find a way of creating the object only when I know the Id, and setting the id to final. This way, more errors can be detected at compile time.
You can simply add a boolean flag, and in your setId(), set/check the boolean. If I understood the question right, we don't need any complex structure/pattern here. How about this:
in this way, if you
setId(0l);
it thinks that the ID is set too. You can change if it is not right for your business logic requirement.not edited it in an IDE, sorry for the typo/format problem, if there was...