Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Enum class using constants  RSS feed

 
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
 
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...
 
Sheriff
Posts: 21459
96
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: 21459
96
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!