We help IT Professionals succeed at work.

Custom Enumerated Type

allelopath
allelopath asked
on
216 Views
Last Modified: 2010-03-30
I would like an enumerated type whose ordinal values is not 1,2,3, ..
e.g. I would like:
    enum MyEnum {
        FIRST = 12,            
        SECOND = 54,
        THIRD = 1134
}

How can this be done?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You can init those values in the constructor. See

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
typedef enum MyEnum {A = 1, B = 15, C = 74, D = 64};
CERTIFIED EXPERT
Top Expert 2016

Commented:
To advise fully though, we'd need to know more about how you intend to use them

Author

Commented:
typedef? This is not C/C++
Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
CEHJ:
Given my initial example, I would like this or something like it):
for (MyEnum myEnum : MyEnum.values()) {
  System.out.println(myEnum);
}
to print out these (as ints, not Strings):
12            
54
1134
>> typedef? This is not C/C++

:-)))))

I'm sorry, my mistake.
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.