Java switch statement: Constant expression required, but it IS constant

前端 未结 13 1429
予麋鹿
予麋鹿 2020-11-22 10:42

So, I am working on this class that has a few static constants:

public abstract class Foo {
    ...
    public static final int BAR;
    public static final          


        
13条回答
  •  长情又很酷
    2020-11-22 11:05

    I recommend you to use enums :)

    Check this out:

    public enum Foo 
    {
        BAR("bar"),
        BAZ("baz"),
        BAM("bam");
    
        private final String description;
    
        private Foo(String description)
        {
            this.description = description;
        }
    
        public String getDescription()
        {
            return description;
        }
    }
    

    Then you can use it like this:

    System.out.println(Foo.BAR.getDescription());
    

提交回复
热议问题