Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get enum custom value?

Tags:

People also ask

Can enums have values?

Overview. The Java enum type provides a language-supported way to create and use constant values. By defining a finite set of values, the enum is more type safe than constant literal variables like String or int.

Can we assign values to enum in Java?

You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once. Therefore, the constructor must be declared private or default. To returns the values of the constants using an instance method(getter).

How do you access enums?

Every enum constant is always implicitly public static final. Since it is static, we can access it by using the enum Name. Since it is final, we can't create child enums. We can declare the main() method inside the enum.


I have an enum like this:

public enum ProductGroup
{
    A = 1,
    B = 2,
    C = 4,
    D = 8
}

How can I get ProductGroup enum values? For example, when my enum value is ProductGroup.C I want get its value 4;