How to pass enum as an argument in a method in java?
How to pass enum as an argument in a method in java?
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())); }
Answer by Narendra Pathai for How to pass enum as an argument in a method in java?
If you want to pass a single value from the enum
public class Test { enum GeneralInformation{ NAME; } private static void print(GeneralInformation val){ System.out.println(val); } public static void main(String[] args) { print(GeneralInformation.NAME); } }
else if you want whole class to be passed then, as it was not clear from the question
private static void print(Class generalInfo){ } public static void main(String[] args) { print(GeneralInformation.class); }
Answer by Robert H for How to pass enum as an argument in a method in java?
An enum is used just like a type:
static void printEnumValue(courseList generalInformation) { // how to receive enum in this block
used like:
printEnumValue(courseList.JAVA);
Answer by Govind Balaji for How to pass enum as an argument in a method in java?
printEnumValue(EnumValues.generalInformation.NAME,EnumValues.generalInformation.AGE,EnumValues.generalInformation.PHONE);
Receiving:
public static void printEnumValue(EnumValues.generalInformation en){ System.out.println(en.toString()); }
Answer by JB Nizet for How to pass enum as an argument in a method in java?
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
.
Answer by A. Markczy for How to pass enum as an argument in a method in java?
Note that your question has mixed up two different problems: Passing an Enum to a function OR Passing an Enum constant to a function. My understanding was that you want to pass the enum itself, not one of it's constants to the function. If not: refer to Narendra Pathai's answer on how to pass a single enum constant to a function. If you don't know what the difference between an enum and an enum constant is, review the docs about enums...
I Understand that what you want is to have a print (or any other) function where you can pass any possible enum, to print each of the enum's possible values (i.e. constants). I've found the following two approaches to do this:
Lets say we have the following enum:
// The test enum, any other will work too public static enum ETest { PRINT,MY,VALUES }
Variante 1: Pass the array of constants from your enum to your function; As the enum's constants are static values, they can easily be accessed and passed to your 'print' function as follows:
public static void main(String[] args) { // retreive all constants of your enum by YourEnum.values() // then pass them to your function printEnum(ETest.values()); } // print function: type safe for Enum values public static > void printEnum(T[] aValues) { System.out.println(java.util.Arrays.asList(aValues)); }
Variante 2: Pass the enum's class as function parameter. This might look more beautiful, but be aware that reflection is involved (performance):
public static void main(String[] args) { printEnum2(ETest.class); } // print function: accepts enum class and prints all constants public static > void printEnum2(Class aEnum) { // retreive all constants of your enum (reflection!!) System.out.println(java.util.Arrays.asList(aEnum.getEnumConstants())); }
In my opinion, it's better to use variante 1 because of the overuse of reflection in variante 2. The only advantage that variante 2 gives you, is that you have the Class object of the Enum itself (the static enum, not only it's constants) inside your function, so I've mentioned it for completeness.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment