I am trying to learn enum in java, i wrote a code to understand the use of valueOf & values, i understood my mistake as the valueOf is case sensitive, so exception was thrown.
But i was surprised to see that output was not in order as i expected, as per my understanding this can only happen when more than one thread is involved, Bur here only one main thread is running. here is my program:
public enum Currency {
PENNY(1) {
@Override
public String color() {
return "bronze";
}
},
NICKLE(5) {
@Override
public String color() {
return "bronze";
}
}, DIME(10) {
@Override
public String color() {
return "silver";
}
}, QUARTER(25) {
@Override
public String color() {
return "silver";
}
};
private int value;
public abstract String color();
private Currency(int value) {
this.value = value;
}
public static void main(String[] args) {
for(Currency i: Currency.values()){
System.out.println(i.toString());
}
System.out.println(Currency.valueOf("penny"));
}
}
and this is the output i got:
Exception in thread "main" java.lang.IllegalArgumentException: No enum const class learn.enumeration.Currency.penny
at java.lang.Enum.valueOf(Unknown Source)
at learn.enumeration.Currency.valueOf(Currency.java:1)
at learn.enumeration.Currency.main(Currency.java:54)
PENNY
NICKLE
DIME
QUARTER
According to me first for-each loop should be executed and then this exception should appear.When i tried it executing again, i get this result also... it gave me hint of two thread involvement in this scenario... But where did i create the another thread?? Thanks in Advance