Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all possible values of an enum in java? (not knowing the specific Enum) [duplicate]

Tags:

java

enums

I'd like to create a JComboBox that handles the selection of any Enum given to it. For that I need a method to retrieve all the available values of the Enum passed to the JComboBox. As I don't know the specific Enum I can't call EnumType.values(). I could think of some complicated solutions where supported Enums would have to implement some interface I define, but I guess I am missing a simpler, more general solution. What is the way I should go?

like image 888
Se Norm Avatar asked Jul 05 '11 16:07

Se Norm


People also ask

Does enum values () maintain order?

values() is a static method of Enum , which always returns the values in same order.

How do I get a list of all enum values?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

Can enum inherit from another enum Java?

You cannot have an enum extend another enum , and you cannot "add" values to an existing enum through inheritance.

Can == be used on enum?

equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.


1 Answers

Class.getEnumConstants()

like image 91
irreputable Avatar answered Sep 28 '22 03:09

irreputable