My class must have an attribute of type string which can have just four values :
value1, value2, value3, or value4 ;
How it can be done in java ?
My class must have an attribute of type string which can have just four values :
value1, value2, value3, or value4 ;
How it can be done in java ?
It can be done a few different ways. This is the most direct (and probably ugly) way to do it, because you only have four eventualities.
private String someString;
public void setSomeString(String inString){
if(inString.equals("valueOne") ||
inString.equals("valueTwo") ||
inString.equals("valueThree")||
inString.equals("valueFour"){
someString = inString;
}else{
//handle this here
}
Use enumerated types, for example