Results 1 to 13 of 13

Thread: Enumeration

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved Enumeration

    Is there any no enumeration in Java? Or something similar. Can't find anything...


    ØØ
    Last edited by NoteMe; Dec 11th, 2004 at 07:47 PM.

  2. #2

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Enumeration

    I haven't checked them out though but probably a lot better than the old way.
    Code:
    /* Enum types the old style */
    
     public class BlenderOldStyle{
    
     public static final int SpeedSetting_off = 0;  
     public static final int SpeedSetting_low = 1;
     public static final int SpeedSetting_med = 2;
     public static final int SpeedSetting_high = 3; 
    
     }

  4. #4
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    Re: Enumeration

    Code:
    public class test{
        public enum morons{noteme,
            mendhak,
            dsheller,
            electroman,
            plenderj,
            unfortunately_me,
            etcetera};
        public test(){
            System.out.println(morons.noteme.toString());
        }
        public static void main(String[] args){
            new test();
        }
    }

  5. #5
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Enumeration

    November issue 11 vol 9 of JDJ has an interesting article on enums in jdk5.0. After getting half way through it seems that some ofthe sorce code is not listed in the magazine(Listings 2-9 can be dl from http://sys-con.com/story/?storyid=46981&DE=1#RES) but the link contains the full article and the source. Give it a read. It's sounds interesting.

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Enumeration

    I wanted to try out how enums work in the jdk5.0 so i did the following. Don't know if this is the right way to use them but i took a shot anyway.
    Code:
    /* Enum types the old style */
    /*
     class BlenderOldStyle{
    
     public static final int SpeedSetting_off = 0;  
     public static final int SpeedSetting_low = 1;
     public static final int SpeedSetting_med = 2;
     public static final int SpeedSetting_high = 3; 
     private int currentsetting = SpeedSetting_off; 
    
     public void setSetting(int setting){
      currentsetting = setting; 
     }
    
     public void checkCurrentSetting(){
      printCurrentSetting(currentsetting); 
     }
    
     private void printCurrentSetting(int currentsetting){
       switch(currentsetting){
        case 0:
        System.out.println("Blender is off");
        break; 
        case 1:
        System.out.println("Blender is on low"); 
        break; 
        case 2:
        System.out.println("Blender is on med"); 
        break; 
        case 3:
        System.out.println("Blender is on high");
        break;  
       }
      }
     }
     
     public class BlenderX{
      public static void main(String[] args){
    
       BlenderOldStyle bos = new BlenderOldStyle(); 
       bos.checkCurrentSetting(); 
       bos.setSetting(bos.SpeedSetting_med); 
       bos.checkCurrentSetting();
      }
     }
    */
    /* new style jdk5.0 */
     class Blender{
      public enum SpeedState{Off,Low,Med,High};
      private SpeedState currentState = SpeedState.Off;
    
      public void setState(SpeedState state){
        currentState = state ;
      }
       public SpeedState getNextSpeed(){
        switch (currentState) {
         case Off: return SpeedState.Off;
         case Low: return SpeedState.Low;
         case Med: return SpeedState.Med;
         case High: return SpeedState.High;
        }
        return null ;
       }
      public void printStates() {
       for(SpeedState state: SpeedState.values()){
         System.out.println(state);
       }
      }
     }
     public class BlenderX{
      public static void main(String[] args){
       Blender bl = new Blender(); 
       System.out.println(bl.getNextSpeed());
       bl.setState(Blender.SpeedState.Med);
       System.out.println(bl.getNextSpeed());
      }
     }

  7. #7

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Enumeration

    Yeah, that was intresting, thanks. But they don't seem to have 5.0 at school, so no help actualy. So I had to do it the hard way. Thanks anyway.



    ØØ

  8. #8
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Enumeration

    I actualy think using static final fields is a lot easier even if it does lack some of the benefits of having an enum type.

  9. #9

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Enumeration

    Quote Originally Posted by Dilenger4
    I actualy think using static final fields is a lot easier even if it does lack some of the benefits of having an enum type.
    Much more to write, and I am used with Enums from C++, so it kind of irritates me. But nothing to do about it.


    ØØ

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Enumeration

    Enums provide type safety, string conversion, less writing, what else do you want?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Enumeration

    Posted by NoteMe

    Much more to write, and I am used with Enums from C++, so it kind of irritates me. But nothing to do about it.
    Sure there is somthing to do about it. Just use enums from java 5.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Enumeration

    Dilenger, he said he can't use Java 5 at school.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width