enum generalInformation { NAME { @Override public String toString() { return "Name"; } }, EDUCATION { @Override public String toString() { return "Education"; } }, EMAIL { @Override public String toString() { return "Email"; } }, PROFESSION { @Override public String toString() { return "Profession"; } }, PHONE { @Override public String toString() { return "Phone"; } } }
I have that information are avaiable in enum.
print.generalInformation
?That outputs:
Name
Education
Phone
generalInformation
as an arg in another function? By default, when you print an enum constant, it prints its literal value e.g. if the name of the enum instance is RED, then it will print RED. This is also the value that is returned by the name() method of java. lang. Enum class.
You can use name() to print the constant name of the enum: Returns the name of this enum constant, exactly as declared in its enum declaration.
Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.
valueOf. Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
System.out.println(java.util.Arrays.asList(generalInformation.values()));
Your second part... Just the same as an interface
or a class
Firstly, I would refactor your enum to pass the string representation in a constructor parameter. That code is at the bottom.
Now, to print all enum values you'd just use something like:
// Note: enum name changed to comply with Java naming conventions for (GeneralInformation info : EnumSet.allOf(GeneralInformation.class)) { System.out.println(info); }
An alternative to using EnumSet
would be to use GeneralInformation.values()
, but that means you have to create a new array each time you call it, which feels wasteful to me. Admittedly calling EnumSet.allOf
requires a new object each time too... if you're doing this a lot and are concerned about the performance, you could always cache it somewhere.
You can use GeneralInformation
just like any other type when it comes to parameters:
public void doSomething(GeneralInformation info) { // Whatever }
Called with a value, e.g.
doSomething(GeneralInformation.PHONE);
Refactoring using a constructor parameter
public enum GeneralInformation { NAME("Name"), EDUCATION("Education"), EMAIL("Email"), PROFESSION("Profession"), PHONE("Phone"); private final String textRepresentation; private GeneralInformation(String textRepresentation) { this.textRepresentation = textRepresentation; } @Override public String toString() { return textRepresentation; } }
With your current values, you could actually just convert the name to title case automatically - but that wouldn't be very flexible for the long term, and I think this explicit version is simpler.
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