Create class enum

I have the following enum class,

public enum Sample { READ, WRITE } 

and in the next class I try to check 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 specify Sample.READ , then passing it as a parameter to the testEnumSample method. Instead, if we want to instantiate an enum class and pass it as a parameter, what do we need to do?

+8
java enums
source share
8 answers

Here I need to specify Sample.READ to pass it as a parameter. Instead, if we want to instantiate an enum class and pass it as a parameter, what do we need to do?

What would "create an instance of the enum class" even mean? The point of enumeration is that there is a fixed set of values ​​- you cannot create later. If you want to do this, you should not use an enumeration.

There are other ways to get enumeration values. For example, you can get the first declared value:

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

or perhaps pass a name and use Sample.valueOf :

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

If you have explained what you are trying to achieve, this will help you more easily.

+22
source share

Inside, the listings 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, eg getter } 

They should not and cannot be initialized.

+12
source share

Enums does not support public constructors and therefore cannot be created. Enumerations are intended for those cases when you have a fixed set of related constants. Exactly one instance will be created for each enumeration constant.

+7
source share

You cannot create a new instance of enum. Otherwise, it will not be an enumeration. You can reference an existing listing. As in your example

  Sample sample = Sample.READ; 
+2
source share

Elements in Enum are objects that are instances of a class.

You do not need to create an Enum object.

Here is a similar problem

Refer to this question

+1
source share

Complete a good Java fundamentals book and read it.

In any case, enumerations in java are classes with a fixed number of objects, and objects are determined by its attributes.

Again you can read something about it here.

+1
source share

Check out my answer in another post .

There are two ways:

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

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

Use the class.getEnumConstants() function to get a list of 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 } 
+1
source share

I answer the first question. You can do it as follows:

 public static void main(String[] args) { Sample.READ.testEnumSample(); } public enum Sample { READ, WRITE; public void testEnumSample() { System.out.println(this); } } 
0
source share

All Articles