Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Nesting enums?  RSS feed

 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was wondering whether it is possible to nest enums?

My problem is that I want an enum called country and then I want a nested enum within that called state/province, is this possible?

so you could do a call maybe something like this:

State state = Country.US.State.NY;

Thanks in advance,

John
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you definitely can nest enums. But, I don't think you can do what you want. You can mess around with your enum definitions to pass in an enum class (that has the states you're interested in). But at that point, it's a runtime issue and I don't think there's any way you can reference them statically in your code. (You'd have to resort to methods to get at the states you're interested in.)
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Bartlett:
Hi,

I was wondering whether it is possible to nest enums?

My problem is that I want an enum called country and then I want a nested enum within that called state/province, is this possible?

so you could do a call maybe something like this:

State state = Country.US.State.NY;

Thanks in advance,

John


Not in the way you want. Realize that your Country enum is a full-fledged class, 1.5 just provides some syntactic sugar to do what we've been doing for a decade. If you were to declare a State enum nested within Country then that would be a nested class like any other. Country.US.State.NY is probably syntactically possible, but it would mean that Country.CANADA.State.NY was just as valid. US and CANADA are both a Country, and State would just be an (implicitly) static nested class of Country.

Perhaps you should describe the "big picture" and we might have some better design suggestions for your problem.
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am currently working on a tax related problem. The problem i am trying to overcome is the abilbity to have countries where there are provinces with different tax rates and countries that donot have seperate provinces.

I currently have a Tax Rate class that contains a reference to a Region Class that has a Country object. (or atleast some variant on that).

Any ideas on a better solution would be very appreciated.

Thank you,

John
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you can have a State enum where the states know their countries, should that help. Or a Country enum where each country knows what states it has, should that help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!