19

Consider I am having the following enum class,

public enum Sample {
    READ,
    WRITE
}

and in the following class I am trying to test the enum class,

public class SampleTest {
    public static void main(String[] args) {
        testEnumSample(Sample.READ);
    }

    public static void testEnumSample(Sample sample) {
        System.out.println(sample);
    }
}

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Hariharan
  • 881
  • 1
  • 13
  • 25

9 Answers9

39

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Is that every item in an enum is instance of enum. I am not able to understand it properly. – Hariharan May 31 '13 at 06:59
  • 1
    @harinewton: yes, that's it. Your code sample shows it. The `testEnumSample` method takes an instance of `Sample` as argument, and you pass it `Sample.READ`, which is an instance of `Sample`. – JB Nizet May 31 '13 at 07:01
  • Is that every enum item is self referential to that enum class. If so is there any reason? – Hariharan May 31 '13 at 07:01
  • 1
    @harinewton: Yes, every name you specify in an enum becomes a public static final field in that enum, of that type. Those are the *only* instances of the enum. The reason is that that's the *purpose* of an enum: providing a fixed set of values. – Jon Skeet May 31 '13 at 07:01
20

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.

Marc
  • 6,051
  • 5
  • 26
  • 56
13

Enums doesn't support public constructors and hence, cannot be instantiated. Enums are for when you have a fixed set of related constants. Exactly one instance will be created for each enum constant.

Maveňツ
  • 1
  • 12
  • 50
  • 89
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
5

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
}
Mavlarn
  • 3,807
  • 2
  • 37
  • 57
3

You cannot create a new enum instance. Otherwise it won't be an enum. You can reference an already existing enum. As in your example

 Sample sample = Sample.READ;
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
1

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

Community
  • 1
  • 1
MaheshVarma
  • 2,081
  • 7
  • 35
  • 58
1

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

ajain
  • 444
  • 4
  • 7
0

I'm answering to the first question. You can easily do it like this:

public static void main(String[] args) {

    Sample.READ.testEnumSample();
}

public enum Sample {
    READ,
    WRITE;

    public void testEnumSample() {
        System.out.println(this);
    }
}
-3

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

vineet
  • 336
  • 2
  • 11