Closest you can get
1) create an Enum for your range of values, for which enumerations are intended.
public Enum MyVals { ONE(1) TWO(2) ... private int val; MyVals(int val) { this.val = val; } }
see this . Of course, this will only work if the values ββare discrete (i.e. don't float)
2) make the field private and write a smart setter that explodes with an invalid value.
public void setVal(int val) { if (val < 0 || val > 10) throw....; this.val = val; }
source share