Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass enum as an argument in a method in java?

Tags:

java

enums

public class Enumvalues{      enum courseList {         JAVA,         C,         PYTHON,         PERL     }      enum generalInformation {         NAME,         AGE,         PHONE     }        enum sex {         MALE,         FEMALE     } }  public static void main(String args[]) {      printEnumValue(generalInformation); // how to pass enum in this block }   static void printEnumValue(enum generalInformation) { // how to receive enum  in this block         System.out.println(java.util.Arrays.asList(generalInformation.values())); } 
like image 764
Reegan Miranda Avatar asked Jan 19 '13 12:01

Reegan Miranda


People also ask

How do you pass an enum in a method argument?

Change the signature of the CreateFile method to expect a SupportedPermissions value instead of plain Enum. Show activity on this post. Show activity on this post. First change the method parameter Enum supportedPermissions to SupportedPermissions supportedPermissions .

Can enum be declared inside a method Java?

We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.

Can you declare enum in method?

Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.

Can enum pass variables in Java?

So no, you can't have different values of a specific enum constant.

What is an enum in Java?

An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces). Why And When To Use Enums?

How do I pass a parameter to an enum value?

enums are technically descendants of Enum class. So, if you want to use only Enum's standard methods in your method (such as values()), pass the parameter like this: static void printEnumValue(Enum generalInformation)See, that the only thing you have to change is the capital letter E. Share Improve this answer Follow

How to loop through the constants of an enum in Java?

The enum type has a values () method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum: An enum can, just like a class, have attributes and methods.

How to use ENUM's standard methods in a method?

So, if you want to use only Enum's standard methods in your method (such as values()), pass the parameter like this: static void printEnumValue(Enum generalInformation)See, that the only thing you have to change is the capital letter E. Share Improve this answer Follow answered Jul 2 '21 at 9:45


1 Answers

An enum is a class. So you can pass an instance of the class (EnumValues.generalInformation.PHONE for example), or you can pass the class itself (EnumValues.generalInformation.class for example).

If you want to list all the instances of an enum class, you need to pass the enum class, and use EnumSet.allOf(EnumValues.generalInformation.class) for example.

Your confusion principally comes from the fact that you don't respect the Java naming conventions. ENums are classes and should start with an upper-case letter (GeneralInformation for example). An other source of confusion is the bad choice of names. JAVA is not a course list. It's a course. So the enum should be named Course.

like image 172
JB Nizet Avatar answered Oct 16 '22 00:10

JB Nizet