Is it possible to create Enum types at Runtime? What are the implications?

Discussion in 'Java' started by Sideswipe, Aug 3, 2007.

  1. Sideswipe

    Sideswipe Guest

    So, I want the nicities of an enum, but I would like to extend the
    enum list at runtime. is such a thing possible?

    Example:

    public enum Day {
    MONDAY("Sucks");
    private String comment;
    Day(String comment) {this.comment = comment;}

    }

    // create a new Day enum Day.TUESDAY
    // then

    switch(day) {
    case TUESDAY:
    ....
    }

    I am nearly certain it isn't possible, but I thought I would ask
    anyways.

    Christian Bongiorno
    http://christian.bongiorno.org
     
    Sideswipe, Aug 3, 2007
    #1
    1. Advertising

  2. Sideswipe

    Piotr Kobzda Guest

    Re: Is it possible to create Enum types at Runtime? What are theimplications?

    Sideswipe wrote:
    > So, I want the nicities of an enum, but I would like to extend the
    > enum list at runtime. is such a thing possible?
    >
    > Example:
    >
    > public enum Day {
    > MONDAY("Sucks");
    > private String comment;
    > Day(String comment) {this.comment = comment;}
    >
    > }
    >
    > // create a new Day enum Day.TUESDAY
    > // then
    >
    > switch(day) {
    > case TUESDAY:
    > ....
    > }
    >
    > I am nearly certain it isn't possible, but I thought I would ask
    > anyways.


    Assuming that's possible, how would you compile the above without a
    TUESDAY present at compile time?


    piotr
     
    Piotr Kobzda, Aug 3, 2007
    #2
    1. Advertising

  3. Sideswipe

    Daniel Pitts Guest

    On Aug 3, 3:02 pm, Sideswipe <> wrote:
    > So, I want the nicities of an enum, but I would like to extend the
    > enum list at runtime. is such a thing possible?
    >
    > Example:
    >
    > public enum Day {
    > MONDAY("Sucks");
    > private String comment;
    > Day(String comment) {this.comment = comment;}
    >
    > }
    >
    > // create a new Day enum Day.TUESDAY
    > // then
    >
    > switch(day) {
    > case TUESDAY:
    > ....
    >
    > }
    >
    > I am nearly certain it isn't possible, but I thought I would ask
    > anyways.
    >
    > Christian Bongiornohttp://christian.bongiorno.org


    If you need to create new cases at runtime, you almost need to have a
    scripting language to create/handle the new cases.
     
    Daniel Pitts, Aug 3, 2007
    #3
  4. Sideswipe

    Roedy Green Guest

    On Fri, 03 Aug 2007 22:02:21 -0000, Sideswipe
    <> wrote, quoted or indirectly quoted
    someone who said :

    >So, I want the nicities of an enum, but I would like to extend the
    >enum list at runtime. is such a thing possible?


    You probably won't like my answer, but you can compose Java source on
    the fly and compile it, or compose class files on the fly.

    See http://mindprod.com/jgloss/onthefly.html

    Enum's are funny things. They don't let you add or remove
    possibilities. They don't let you extended the methods.
    There is Enumset though to play with various groups of enum constants.
    see http://mindprod.com/jgloss/enumset.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Aug 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    547
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    468
    Rolf Magnus
    Nov 13, 2003
  4. mrhicks
    Replies:
    2
    Views:
    427
    Dave Thompson
    Jun 10, 2004
  5. shapper

    Create Enum at runtime

    shapper, Jun 26, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    496
    Masudur
    Jun 26, 2007
Loading...

Share This Page