Understanding Enums in Java

后端 未结 6 1896
Happy的楠姐
Happy的楠姐 2020-11-27 04:04

What are java enums? How do they work? Where could I used them and how?
Can I do without using enums in an app or are they so powerful that Its better to use them than i

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 04:43

    Think of Enum as follows

    public class MyEnum {
    
        // Object instantiated at declaration
        public static final MyEnum ONE = new MyEnum();
        public static final MyEnum TWO = new MyEnum();
        public static final MyEnum THREE = new MyEnum();
    
        // Notice a private constructor 
        // There is no way outside MyEnum class call it
        private MyEnum() { ... }
    
    
    }
    

    So a MyEnum as a enum would be

    public enum MyEnum {
        ONE,
        TWO,
        THREE;
    }
    

    Both are similar

    regards,

提交回复
热议问题