How to use enum that are not int type in the switch( ) statement in java?

Discussion in 'Java' started by Bruce Sam, Jan 10, 2005.

  1. Bruce Sam

    Bruce Sam Guest

    /* ******************************* */
    class PDomainEnum {
    public static final int PDPoint = 0;
    public static final int PDLine = 1;
    }

    class PDomain {
    PDomainEnum type;

    public void within(type) {
    switch(type) {
    case PDPoint:
    break;
    case PDLine:
    break;
    default:
    break;
    }
    }
    }
    /* ************************** */
    The above is a not a completed program but just to show
    something.I want to create an enum PDomainEnum,it has two variable
    PDPoint and PDLine.In PDomain,I define an object of PDomainEnum
    type.The most important thing that I want to do is use the switch( ) to
    select statement.I know switch not allow the non-int type in it.But I
    can't find a method to implement the selection statement.Could you give
    me some advice?Addtional,I'm using J2SE 1.4.2,and has some reason I
    can't not use J2SE1.5.
     
    Bruce Sam, Jan 10, 2005
    #1
    1. Advertising

  2. Bruce Sam

    Guest

    The switch statement is just a convenient way of using nested if-else
    statements
     
    , Jan 10, 2005
    #2
    1. Advertising

  3. Re: How to use enum that are not int type in the switch( ) statementin java?

    Bruce Sam wrote:
    > /* ******************************* */
    > class PDomainEnum {
    > public static final int PDPoint = 0;
    > public static final int PDLine = 1;


    > }
    >
    > class PDomain {
    > PDomainEnum type;
    >
    > public void within(type) {
    > switch(type) {
    > case PDPoint:
    > break;
    > case PDLine:
    > break;
    > default:
    > break;
    > }
    > }
    > }
    > /* ************************** */
    > The above is a not a completed program but just to show
    > something.I want to create an enum PDomainEnum,it has two variable
    > PDPoint and PDLine.In PDomain,I define an object of PDomainEnum
    > type.


    This is the problem, you should be defining an int, see below.

    The most important thing that I want to do is use the switch( ) to
    > select statement.I know switch not allow the non-int type in it.But I
    > can't find a method to implement the selection statement.Could you give
    > me some advice?Addtional,I'm using J2SE 1.4.2,and has some reason I
    > can't not use J2SE1.5.
    >


    Your problem is that the 'within' method takes an instance of
    PDomainEnum Class. Yet the PDomainEnum class is not your enum, but just
    the class where the enums PDPoint & PDLine are defined.

    so, change the signature of the 'within()' method to accept an 'int' and
    call it like this.

    PDomain p = new PDomain()

    p.within(PDomainEnum.PDLine);



    Having said all of this, your 'enums' are just static int's. Which is
    fine, but very crude. Have a google for the Type-Safe enum idiom.

    Also, google for 'refactoring replace conditional'.

    One of the first links should be...

    http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html
     
    Andrew McDonagh, Jan 10, 2005
    #3
    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. Schnoffos
    Replies:
    2
    Views:
    1,247
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,697
    Old Wolf
    Jan 20, 2004
  3. Replies:
    21
    Views:
    1,078
    Giannis Papadopoulos
    Aug 2, 2005
  4. enum type int or unsigned int?

    , Apr 13, 2006, in forum: C Programming
    Replies:
    10
    Views:
    1,921
    Michael Mair
    May 2, 2006
  5. army1987
    Replies:
    2
    Views:
    638
    Victor Bazarov
    Sep 5, 2012
Loading...

Share This Page