Instantiate enum class

后端 未结 9 1504
南笙
南笙 2020-12-03 05:34

Consider I am having the following enum class,

public enum Sample {
    READ,
    WRITE
}

and in the following class I am trying to test th

相关标签:
9条回答
  • 2020-12-03 05:56

    Check my answer in another post.

    There are 2 ways:

    Use Enum.valueOf() static function, then cast it into your enum type.

    Enum v = Enum.valueOf(TheEnumClass, valueInString);
    

    Use class.getEnumConstants() function to get the list of the enum constants, and loop this list and get.

    Plugins[] plugins = Plugins.class.getEnumConstants();
    for (Plugins plugin: plugins) {
        // use plugin.name() to get name and compare
    }
    
    0 讨论(0)
  • 2020-12-03 05:58

    You cannot instantiate an Enum, thats the whole point of having an Enum. For example you would use enum when defining properties of some thing which would never change like:

    enum TrafficLight {
         RED("stop"),
         YELLOW("look"),
         GREEN("go")
    }
    

    private string value;

    private TrafficLight(String value) {
        this.value = value;
    }
    
    public getValue() {
        return value;
    }
    

    Now if you want to get the value of the enum, you can use valueOf method of enums. The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though;

    TrafficLight trafficLight = TrafficLight.valueOf("RED")
    String value = trafficLight.getValue();
    

    If you do TrafficLight.valueOf("XYZ"), it will throw an IllegalArgumentException

    0 讨论(0)
  • 2020-12-03 06:00

    Here I need to specifying Sample.READ to pass it as parameter. Instead if we want to instantiate the enum class and pass it as parameter what we need to do?

    What would "instantiate the enum class" even mean? The point of an enum is that there are a fixed set of values - you can't create more later. If you want to do so, you shouldn't be using an enum.

    There are other ways of getting enum values though. For example, you could get the first-declared value:

    testEnumSample(Sample.values()[0]);
    

    or perhaps pass in the name and use Sample.valueOf:

    testEnumSample("READ");
    
    ...
    
    Sample sample = Sample.valueOf(sampleName);
    

    If you explained what you were trying to achieve, it would make it easier to help you.

    0 讨论(0)
  • 2020-12-03 06:03

    Fetch a good book on Java Basics and read it.

    Anyways, Enums in java are classes with fixed number of objects and objects are defined by its attributes.

    Again, you can read something about it here

    0 讨论(0)
  • 2020-12-03 06:08

    Internally, enums will be translated to something like this

    class Sample extends Enum {
        public static final Sample READ = new Sample("READ", 0);
        public static final Sample WRITE = new Sample("WRITE", 1);
    
        private Sample(String s, int i)
        {
            super(s, i);
        }
    
        // More methods, e.g. getter
    }
    

    They should not and cannot be initialized.

    0 讨论(0)
  • 2020-12-03 06:09

    The elements within an Enum are objects that are instances of the class.

    You no need to create an object of Enum.

    Here is a similar issue

    Refer this

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