?
Solved

Custom Enumerated Type

Posted on 2007-10-08
9
Medium Priority
?
203 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?
0
Comment
Question by:allelopath
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20035037
You can init those values in the constructor. See

http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20035038
typedef enum MyEnum {A = 1, B = 15, C = 74, D = 64};
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20035054
To advise fully though, we'd need to know more about how you intend to use them
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:allelopath
ID: 20035068
typedef? This is not C/C++
0
 
LVL 5

Accepted Solution

by:
mkatmonkey earned 1600 total points
ID: 20035073
In java 1.5 you can associate a value with an enumeration. However, to do exactly what your asking is only possible in c++.


public enum Base{
    FIRST(12),
    SECOND   (54),
    THIRD  (1134)

    private final int aValue;   //

    Base(int aValue) {
        this.aValue= aValue;
    }
    public int getValue()   { return aValue; }
}

This  site has some decent examples too:

 http://mooch.sourceforge.net/toolTalk2.php?style=style:
0
 
LVL 1

Author Comment

by:allelopath
ID: 20035115
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
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20035129
>> typedef? This is not C/C++

:-)))))

I'm sorry, my mistake.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 20035164
You'd have to do

for (MyEnum myEnum : MyEnum.values()) {
  System.out.println(myEnum.getMyValue());
}

after intializing that per the example
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20035512
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question