This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Nesting enums?

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic