Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Enum class using constants  RSS feed

 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rob Spoor
Sheriff
Posts: 21019
84
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Output:
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool

thanks


Haven't checked back for a while
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21019
84
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
See where your hand is? Not there. It's next to this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!