Here is something a tad bit more clean and re-usable/generic
import java.util.Random;
public class EnumUtil {
public static <T extends Enum<?>> T getRandomEnumValue(Class<T> clazz, Random random) {
int constants = clazz.getEnumConstants().length;
if(constants == 0) {
throw new IllegalArgumentException("Enum class has no constants");
}
return clazz.getEnumConstants()[random.nextInt(constants)];
}
public static void main(String[] args) {
Random random = new Random();
Tab tab = EnumUtil.getRandomEnumValue(Tab.class, random);
}
}