Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1238
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
|---|---|
| From | Lew <lewbloch@gmail.com> |
| Newsgroups | comp.lang.java.help |
| Subject | Re: Dynamic enums |
| Date | Tue, 11 Oct 2011 19:03:46 -0700 (PDT) |
| Organization | http://groups.google.com |
| Lines | 40 |
| Message-ID | <20776956.237.1318385026281.JavaMail.geo-discussion-forums@prdw1> (permalink) |
| References | <4e94eeb1$0$16274$9a566e8b@news.aliant.net> |
| Reply-To | comp.lang.java.help@googlegroups.com |
| NNTP-Posting-Host | 65.50.217.147 |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-Trace | posting.google.com 1318385026 7475 127.0.0.1 (12 Oct 2011 02:03:46 GMT) |
| X-Complaints-To | groups-abuse@google.com |
| NNTP-Posting-Date | Wed, 12 Oct 2011 02:03:46 +0000 (UTC) |
| In-Reply-To | <4e94eeb1$0$16274$9a566e8b@news.aliant.net> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=65.50.217.147; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T |
| User-Agent | G2/1.0 |
| X-Google-Web-Client | true |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.help:1238 |
Show key headers only | View raw
Linus Flustillbe wrote:
> Let's say I have an ENUM defined as
>
> public enum xxx {
> A, B, C, D, E
> }
>
> Now here's my question.
> Let's say I have a table called foobar which has the following rows
>
> A 1
> B 2
> C 3
> D 4
> E 5
>
> My code is written and is working great. The enum xxx is used as a
> lookup on the foobar table. If I add a row to foobar like
>
> F 6
>
> I need to add another enum to the xxx enum and rebuild my project.
>
> So my question is, is there something like a dynamic enun: by this
> could I call a method on the xxx enum like
>
> xxx.addnew("F") that would make F a valid enum of xxx and not have to
> rebuild my project?
You can do it, though I don't have the patience to go into a whole lot of detail just now. You have to sacrifice the built-in conveniences that 'enum' enjoys as a special part of the language, in favor of the flexibility you require.
You write a "type-safe enumeration" class of your own. At a minimum you should support a 'fromString()' and 'toString()' method similar to real enums' 'valueOf()' and 'toString()'.
/Effective Java/, first edition, had a chapter on this that might by googlable. The book itself is in the second edition and that chapter was removed due to the presence of enums in 5 and on.
Essentially you create static immutable instances of your custom class and use a private (or package-private) constructor.
--
Lew
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Dynamic enums Linus Flustillbe <admin@nacs.dyndns-office.com> - 2011-10-12 01:34 +0000
Re: Dynamic enums Lew <lewbloch@gmail.com> - 2011-10-11 19:03 -0700
Re: Dynamic enums Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-11 22:05 -0400
Re: Dynamic enums Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-11 22:11 -0400
Re: Dynamic enums Linus Flustillbe <admin@nacs.dyndns-office.com> - 2011-10-12 06:02 +0000
Re: Dynamic enums markspace <-@.> - 2011-10-11 23:44 -0700
Re: Dynamic enums Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-10-12 07:58 -0400
Re: Dynamic enums markspace <-@.> - 2011-10-12 07:46 -0700
Re: Dynamic enums markspace <-@.> - 2011-10-11 19:32 -0700
Re: Dynamic enums Roedy Green <see_website@mindprod.com.invalid> - 2011-10-12 07:02 -0700
csiph-web