1

This might seem like a stupid question at first, just do field.set(obj, null). But if the field is not an object, but a primitive, this will return an error. What I want is that it "resets" the value, no matter if it is an object, or a primitive (e.g. String -> null, int -> 0, boolean -> false). I have already tried doing field.getType().isPrimitive() ? 0 : null, hoping that the 0 would cast to short, byte, boolean, etc. automatically, but that did not work either. My current solution, a switch statement using field.getType(), is absolute garbage. Is there a cleaner, one-line solution to this?

weird guy
  • 157
  • 1
  • 9
  • 1
    Unsure if this is a duplicate, but a very similar question is here (with answers that should help): https://stackoverflow.com/questions/2891970/getting-default-value-for-primitive-types – kutschkem Feb 28 '22 at 12:02
  • `Array.get(Array.newInstance(field.getType(), 1), 0)` – Johannes Kuhn May 23 '22 at 12:41
  • Does this answer your question? [Getting default value for primitive types](https://stackoverflow.com/questions/2891970/getting-default-value-for-primitive-types) – Johannes Kuhn May 23 '22 at 12:49

0 Answers0