changing the guts of an enum value

Discussion in 'Java' started by Aryeh M. Friedman, May 2, 2005.

  1. Lets say I have something like this:

    // yes this compiles I cut and pasted this from a example I wrote
    // specificallu for this post
    public enum foo {
    Ack(10);

    private foo(int val)
    {
    setVal(val);
    }

    public void setVal(int val)
    {
    Val=val;
    }

    public int getVal()
    {
    return Val;
    }

    private int Val;

    public static void main(String [] arg)
    {
    System.out.println(Ack.getVal());
    Ack.setVal(20);
    System.out.println(Ack.getVal());
    }
    }


    is there any reason why I shouldn't do something like it, assume that
    main() is for debugging only and the real code has the actual user's of
    the enum values calling get/setVal.

    The idea here is I am writting a parser that needs to reconize different
    types of delims (depending on input type) and came up with the idea of
    just redefining an enum thus the code is still symbolic but dynamic.
     
    Aryeh M. Friedman, May 2, 2005
    #1
    1. Advertising

  2. Aryeh M. Friedman

    Alan Moore Guest

    On Sun, 01 May 2005 23:08:02 +0000, "Aryeh M. Friedman"
    <> wrote:

    >Lets say I have something like this:
    >
    >// yes this compiles I cut and pasted this from a example I wrote
    >// specificallu for this post
    >public enum foo {
    > Ack(10);
    >
    > private foo(int val)
    > {
    > setVal(val);
    > }
    >
    > public void setVal(int val)
    > {
    > Val=val;
    > }
    >
    > public int getVal()
    > {
    > return Val;
    > }
    >
    > private int Val;
    >
    > public static void main(String [] arg)
    > {
    > System.out.println(Ack.getVal());
    > Ack.setVal(20);
    > System.out.println(Ack.getVal());
    > }
    >}
    >
    >
    >is there any reason why I shouldn't do something like it, assume that
    >main() is for debugging only and the real code has the actual user's of
    >the enum values calling get/setVal.
    >
    >The idea here is I am writting a parser that needs to reconize different
    >types of delims (depending on input type) and came up with the idea of
    >just redefining an enum thus the code is still symbolic but dynamic.


    I don't see any reason why you shouldn't do that. In the "Typesafe
    Enums" item in _Effective Java_, Josh Bloch encourages using enums as
    "starter" classes, which may eventually evolve into more complex
    classes. That was in reference to his home-grown enum pattern, but I
    think it still applies to the built-in enums (which were also written
    by Bloch, BTW).
     
    Alan Moore, May 2, 2005
    #2
    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:
    549
  2. Jerminia
    Replies:
    3
    Views:
    633
    Roedy Green
    Oct 7, 2005
  3. Replies:
    9
    Views:
    387
    Gordon Beaton
    Mar 21, 2006
  4. kj
    Replies:
    2
    Views:
    105
  5. J.D. Baldwin

    Gisle Aas's Illustrated Perl Guts?

    J.D. Baldwin, Apr 11, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    160
    J.D. Baldwin
    Apr 14, 2008
Loading...

Share This Page