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

public class Test {
    enum Enum {
        ONE("oneInfo"), TWO("twoInfo"), THREE("threeInfo");
        
        private static String info = ""; //1
        
        Enum(String info) {
            this.info = info;     //2
        }
        
        public static String getInfo() {
            return info;
        }
    }

    public static void main(String[] args) {
        System.out.println(Enum.TWO.getInfo()); // 3
    }
}
Explanation
You can't reference static fields in enum's constructor. Compilation error: illegal reference to static field from initializer.
For code to compile you need to remove static keyword from info field and getInfo() menthod.

Follow CodeGalaxy

Mobile Beta

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