I can write my own but I’m guessing this is very standard. Is there a standard java class that does this? I know this has to be a runtime feature because various parts of my code could set it without knowing what each other are doing – I just want this to fail loudly if this happens.
public class MutableOnce<Type T> {
private T _t;
private boolean _isSet = false;
public void set(T t) {
if(_isSet) {
//raise exception
}
_t = t;
public T get() { return _t; }
}
Any reasons why you don’t want to simply make the object immutable? It would make the class much easier to use, especially in a concurrent environment (the class in your example is not thread safe).