Enums in java



  • I'm learning Java for the first time, and I'm encountering some strange behaviour when dealing with enums.

     My first problem is that I have found no way of explicitly assigning values when creating a literal enum.

    An example of what I'm trying to do, by analogy with C:
    enum wtf{
      VALUE = 3,
      OTHERVALUE = 9
    };

    When I compile a class containing enums, I get a file for each enum present, of the form classname$member.clas

     After some googling, I discovered that this is the result of 'inner classes'.  Does this mean that, in Java, the enum keyword is just syntactic sugar for creating a class with default members?  What's going on here?



  •  I'll take this backwards in order the simple stuff first.@davidrhoskin said:

    When I compile a class containing enums, I get a file for each enum present, of the form classname$member.clas

     After some googling, I discovered that this is the result of 'inner classes'.  Does this mean that, in Java, the enum keyword is just syntactic sugar for creating a class with default members?  What's going on here?

    More or less.  The Java tutorials explain it like this:

     

    Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type.
    @davidrhoskin said:

     My first problem is that I have found no way of explicitly assigning values when creating a literal enum.

    An example of what I'm trying to do, by analogy with C:
    enum wtf{
      VALUE = 3,
      OTHERVALUE = 9
    };

    You can do that like this:

    enum WTF {
        VALUE(3),
        OTHERVALUE(9);
    
    private final int _value;
    WTF(int value) {
        _value = value;
    }
    
    public int getValue() { 
        return _value;
    }
    

    };




    Which will allow you to do WTF.OTHERVALUE.getValue() to get back that value of 9.



  •  Thanks for the tutorials link - I hadn't found that part of the java site yet.

     Thanks also for the value assignment code - I think that's helped me understand a bit better how constructors and the like work. (I'm new to OO in general.)  That syntax is scary, though.

    It doesn't help for the specific case I had, where I was trying to use the values both as case labels and array indices; However, preventing abuse like that is arguably a feature. In the spirit of wtf, I wound up running the code through the C preprocessor and using #define for a similar effect.



  • @davidrhoskin said:

     However, preventing abuse like that is arguably a feature.

     

    The makers of Java spent a lot of time making sure their language wasn't very powerful so that we peon code monkeys don't write code they don't consider "good."


Log in to reply