Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Runtime constant

Reply
Thread Tools

Runtime constant

 
 
Andrew McDonagh
Guest
Posts: n/a
 
      08-16-2005
MikL wrote:
> Hi,
>
> Can anyone suggest a workaround for the following problem? I have a
> constant (static, final) whose value is calculated at runtime, when the
> class is loaded. Because it isn't known at compile time, I can't use it in
> a switch statement. But I'd like to.
>
> public class StaticTest
> {
> public final int AAA = 1;
> public final int BBB = 5 + 7;
> public final int CCC = java.lang.String.class.getFields().length;
> // ^-- just an example method
>
> public void doSomething (int aIndex)
> {
> switch (aIndex)
> {
> case AAA: System.out.println("alpha"); break;
> case BBB: System.out.println("bravo"); break;
> case CCC: System.out.println("charlie"); break; // this line
> doesn't compile
> }
> }
> }
>
>



Yep, but I don't have time right now to show the code, just a possible
answer....

replace the conditional logic with polymorphism....

i.e. a map of Command objects
 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      08-17-2005
> Ironically you use enum because they work in switches, but when you
> are done most of your switches disappear, replaced by enum functions.


What do you mean by an "enum function" ? (checked your glossary, you don't
seem to have an entry for this concept)


 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      08-17-2005
jan V wrote:
>>Ironically you use enum because they work in switches, but when you
>>are done most of your switches disappear, replaced by enum functions.

>
> What do you mean by an "enum function" ? (checked your glossary, you don't
> seem to have an entry for this concept)


I assume it means an instance method that happens to be a member of an
enum. You can't go around defining every combination of words. Although
you can legislate against their use.

Tom Hawitn
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      08-17-2005
Andrew McDonagh wrote:
> MikL wrote:
>
>> Hi,
>>


snipped
>
>
> Yep, but I don't have time right now to show the code, just a possible
> answer....
>
> replace the conditional logic with polymorphism....
>
> i.e. a map of Command objects



Excellent, Roland has managed to provide the example code before me
(cheers Roland)

Andrew
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-18-2005
On Wed, 17 Aug 2005 11:12:09 GMT, "jan V" <> wrote or quoted
:

>
>What do you mean by an "enum function" ? (checked your glossary, you don't
>seem to have an entry for this concept)


check out http://mindprod.com/jgloss/enum.html

I did a fairly major update on it late last night.

There are there flavours of enum methods:


1. common code for all, each uses a different constants passed in the
constructor.

2. all enum constants implement a common method a different way.
Constants override an abstract method is the enum itself.

3. some enum constants implement a method, and others do not.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-18-2005
On Wed, 17 Aug 2005 11:12:09 GMT, "jan V" <> wrote or quoted
:

>What do you mean by an "enum function" ?


an instance method defined on an enum constant or possibly a method
defined on an enum.


Sorry, I am old, and ancient terminology creeps through from time to
time. At least I have stopped myself from referring to RAM as "core".

The neat thing about enums you can nearly always get rid of switch
code by defining an enum constant instance method. This consolidates
logic in the enum class where it is easier to maintain, and it
simplifies client logic.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work??? hn.ft.pris@gmail.com C++ 13 01-22-2007 02:03 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57