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

S

Sideswipe

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
 
P

Piotr Kobzda

Sideswipe 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?

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
 
D

Daniel Pitts

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.
 
R

Roedy Green

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top