What is the output of the following application?
package mypkg; // ww w. ja v a2s . c om enum Money { DOLLAR, YEN, EURO } abstract class Bank { protected Money c = Money.EURO; } public class Main extends Bank { protected Money c = Money.DOLLAR; public static void main(String[] v) { int value = 0; switch(new Main().c) { case 0: value--; break; case 1: value++; break; } System.out.print(value); } }
C.
The type of the variable in the switch statement is the enum Money, but the case statements use int values.
While the enum class hierarchy does support an ordinal()
method, which returns an int value, the enum values cannot be compared directly with int values.
The code does not compile,
since the case statement values are not compatible with the variable type in the switch statement, making Option C the correct answer.