I have a Record class as follows
class Record {
String name;
Object value;
public void setValue(Object value) { this.value = value; }
}
If no value was set to the value
field then it would be null
. It is also possible that null
was explicitly set using the setter i.e new Record().setValue(null)
.
Since explicitly setting the value as null
has meaning in my application, I want to track whether the null value was explicitly set by user or is just an implicit null. What is the best way and the proper way to do it?
The solutions I came up with were,
- A boolean flag
- Use a marker class. Set value as
Object value = new Undefined();
and perform instance check later. HereUndefined
is an internal dummy class. - Use
Optional<Object> value;
and have the setter aspublic void setValue(Object value) { this.value = Optional.ofNullable(value); }
If this was JavaScript, it has undefined
built in the language itself. What is the best way to do this Java?