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())); }
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 .
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”.
Declaration of enum in Java: Enum declaration can be done outside a Class or inside a Class but not inside a Method.
So no, you can't have different values of a specific enum constant.
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?
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
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.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With