Separate Enum file won't accept variables

前端 未结 1 836
醉酒成梦
醉酒成梦 2021-01-26 19:36

I\'ve created an enum.java file and I get errors when creating variables. But in my other .java files, none of these errors appear. Within the enum Foo, the only th

1条回答
  •  北海茫月
    2021-01-26 20:01

    You're missing the most important element of the enum: the enum instances.

    public enum Foo
    {
        //  instances go here
    
        ;   // **** semicolon needed
    
        private String foo;
        private boolean isBarable;
    
        private Foo(String foo, boolean isBarable)
        {
            this.foo = foo;
            this.isBarable = isBarable;
        }
    }
    

    Shoot, just adding the semicolon alone would solve your compilation error, but without the enum instances, the enum is useless.

    e.g.,

    public enum Foo
    {
        BAR("bar", true), BAZ("baz", false) ;
    
        private String foo;
        private boolean isBarable;
    
        private Foo(String foo, boolean isBarable)
        {
            this.foo = foo;
            this.isBarable = isBarable;
        }
    }
    

    0 讨论(0)
提交回复
热议问题