What will be the result of compilation and execution of the following code?

public class Test {    
    enum EnumTest {
        VALUE1 {
            public EnumTest getValue() {
                return VALUE2.getValue().getValue();
            }
        },
        
        VALUE2 {
            public EnumTest getValue() {
                return VALUE1;
            }
        };
        
        abstract EnumTest getValue();
    }    
    
    public static void main(String... args) {
        System.out.println(EnumTest.VALUE1);
        System.out.println(EnumTest.VALUE2);
        System.out.println(EnumTest.VALUE1.getValue());
        System.out.println(EnumTest.VALUE2.getValue());
    }
}
Explanation
Infinite recursion will lead to StackOverflowError:
VALUE1
VALUE2
Exception in thread "main" java.lang.StackOverflowError at Test$EnumTest$2.getValue(Test.java:11) at Test$EnumTest$1.getValue(Test.java:5) at Test$EnumTest$1.getValue(Test.java:5)

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes