| Author |
Enum class using constants
|
Clay Chow
Ranch Hand
Joined: Nov 09, 2008
Posts: 35
|
|
My Code : public enum ExpenseCategory1 { NECESSITY(0,rangeIncrement), LUXURY(1000001, 2000000); static final int rangeIncrement = 1000000; private int startRange; private int endRange; ExpenseCategory1(int a, int b) { this.startRange = a; this.endRange = b; } public int getStartRange() { return startRange; } public int getEndRange() { return endRange; } } Error: Illegal Forward Reference when I attempt to refer to 'illegal forward reference' Question: how would I implement this (using a constant in the enum declaration? ) thanks
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Welcome to JavaRanch! I don't know the "best" way to handle this. There's probably a pattern that would apply nicely, so perhaps another rancher will offer a more elegant solution. In the meantime, one thing you could do is define a nested interface for your constants...
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Output:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Clay Chow
Ranch Hand
Joined: Nov 09, 2008
Posts: 35
|
|
Cool thanks Haven't checked back for a while
|
 |
Clay Chow
Ranch Hand
Joined: Nov 09, 2008
Posts: 35
|
|
Actually got another question about enums: say I have an enum declared as: does each value have a corresponding int value ? (like A = 1, B= 2). I'm asking because I want to convert an int to the enum type, such that the following will compile: thanks
|
 |
Clay Chow
Ranch Hand
Joined: Nov 09, 2008
Posts: 35
|
|
I found one way, not sure if there is a more elegant way to do it. It does seem a bit silly, but I have a method that receives user input (in the form of an int).
Originally posted by Clay Chow: Actually got another question about enums: say I have an enum declared as: does each value have a corresponding int value ? (like A = 1, B= 2). I'm asking because I want to convert an int to the enum type, such that the following will compile: thanks
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
That seems to be the best way to do it. I didn't even think of that - I was about to suggest getting values() and then iterating over it. But that's completely unnecessary of course And next time, instead of creating several posts below each other with just a few minutes in between, could you please use the edit button ( ) the next time to edit your last post?
|
 |
 |
|
|
subject: Enum class using constants
|
|
|