changing the guts of an enum value

  • Thread starter Aryeh M. Friedman
  • Start date

A

Aryeh M. Friedman

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

Advertisements

A

Alan Moore

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

Top