This discussion is archived
12 Replies Latest reply: Nov 27, 2012 5:24 AM by aksarben RSS

Enum

814889 Newbie
Currently Being Moderated
HI all

What is the real need of enum class what i basically understand about enum is that is acts as constant

Edited by: Vicky on Nov 14, 2012 4:30 AM
  • 1. Re: Enum
    gimbal2 Guru
    Currently Being Moderated
    Many uses, also a few for which it wasn't really designed. But a good reason to use it is to have constants, yes.

    I also like to use it in web environments in multiselect fields (such as a dropdown selection box) where you have a known set of values. An example:
    public enum AddressType {
      HOME("Home address"),
      POSTAL("Postal address");
    
      private String label;
    
      private AddressType(String label){
         this.label = label;
      }
    
      public String getLabel(){
        return label;
      }
    }
    This allows me to easily construct select items with the enum constant as the value and the constant's label as the text displayed to the user. Values and labels are neatly managed all within the same enum.
  • 2. Re: Enum
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Instead of
    switch(index){
      case 0: // do something
      case 1: // do something else
    }
    you could write
    enum Action {
      CASE0 {  void do() {/*  do something */ },
      CASE1 {  void do() {/*  do something else*/ };
      abstract void do();
    }
    //...
    Action.values()[index].do();
    *[edit]* BTW: the enum could extend an interface so that the declaration of methods implemented could be somewere else available for all enums serving the same purpose...

    bye
    TPD

    Edited by: TPD Opitz-Consulting com on 14.11.2012 13:59
  • 3. Re: Enum
    Kayaman Guru
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    Instead of
    switch(index){
    case 0: // do something
    case 1: // do something else
    }
    you could write
    enum Action {
    CASE0 {  void do() {/*  do something */ },
    CASE1 {  void do() {/*  do something else*/ };
    abstract void do();
    }
    I'd say that's a horrible example of what enums are good for.
  • 4. Re: Enum
    Jörg Explorer
    Currently Being Moderated
    What is the real need of enum class ...?
    http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html
  • 5. Re: Enum
    gimbal2 Guru
    Currently Being Moderated
    Jörg wrote:
    What is the real need of enum class ...?
    http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html
    He, RTFM :) And here's us trying to be nice for a change, dishing out examples left and right ;)
  • 6. Re: Enum
    Jörg Explorer
    Currently Being Moderated
    Yeah. The manual just adds some more examples. ;-)
  • 7. Re: Enum
    kaja.mohideen Newbie
    Currently Being Moderated
    In Simple terms - Enum is the feature using which you can limit the number of instances of your class and each instance can be given a constant name.

    Usually, we create multiple instances of class (as Enum Constants) when two instances are not equal to one another and those constants can be used for polymorphic invocations if your class is implementation of an interface.
  • 8. Re: Enum
    EJP Guru
    Currently Being Moderated
    Enum is the feature using which you can limit the number of instances of your class
    That's one of the features. i wouldn't have listed it first in a million years.
    and each instance can be given a constant name.
    That's what I would have listed first. Second, I would have listed the fact that Enum members have class-like behaviour as well as instance-like behaviour, IOW they can declare member variables and functions, berried functions, etc.
    Usually, we create multiple instances of class (as Enum Constants) when two instances are not equal to one another
    Meaningless. All Enum instances are different from each other by definition.
    and those constants can be used for polymorphic invocations if your class is implementation of an interface.
    Or even if it isn't; even for example if it is just a member of an Enum, which is what we are talking about.

    Undistinguished contribution.
  • 9. Re: Enum
    kaja.mohideen Newbie
    Currently Being Moderated
    EJP wrote:
    Enum is the feature using which you can limit the number of instances of your class
    That's one of the features. i wouldn't have listed it first in a million years.
    Maybe, the term 'feature' is bit overloaded here. But, I couldn't understand why it cannot be listed. Just because it statically limits the number by code!
    and each instance can be given a constant name.
    That's what I would have listed first. Second, I would have listed the fact that Enum members have class-like behaviour as well as instance-like behaviour, IOW they can declare member variables and functions, berried functions, etc.
    Giving an instance - a name with which it can be referred by is not very special as it is again the constant defined by the developer just like variables referring instances.

    BTW, the constant-specific body can be used to code what you need. But, the main objective of allowing that in JLS is to make your instance provide more sophisticated implementation.
    Usually, we create multiple instances of class (as Enum Constants) when two instances are not equal to one another
    Meaningless. All Enum instances are different from each other by definition.
    Maybe, but if there is only no-args constructor and no constant-specific code - all the instances are going to behave in the same way.
    and those constants can be used for polymorphic invocations if your class is implementation of an interface.
    Or even if it isn't; even for example if it is just a member of an Enum, which is what we are talking about.

    Undistinguished contribution.
    Enum class instances are meant to behave differently from other instances of the same class. If its not by interface, atleast with their cardinal order they should. Am I right?
  • 10. Re: Enum
    EJP Guru
    Currently Being Moderated
    I couldn't understand why it cannot be listed.
    Nobody said that. I said I wouldn't have listed it first.
    BTW, the constant-specific body can be used to code what you need.
    No kidding.
    But, the main objective of allowing that in JLS is to make your instance provide more sophisticated implementation.
    Do you have any evidence for that assertion?
    Usually, we create multiple instances of class (as Enum Constants) when two instances are not equal to one another
    Meaningless. All Enum instances are different from each other by definition.
    Maybe, but if there is only no-args constructor and no constant-specific code - all the instances are going to behave in the same way.
    Either they are all 'going to behave in the same way' or they are 'usually ... not equal to one another'. Make up your mind. These are your statements. You make sense of them. I can't.
    Enum class instances are meant to behave differently from other instances of the same class.
    Except that 'if there is ... no constant-specific code' they don't. Again, make up your mind.
    If its not by interface, atleast with their cardinal order they should. Am I right?
    I don't know why you're asking me to resolve your own self-contradictions.
  • 11. Re: Enum
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    kaja.mohideen wrote:
    Maybe, but if there is only no-args constructor and no constant-specific code - all the instances are going to behave in the same way.
    Meaybe that's just a wording thing.

    But I for myself want all instances of a classe to <i>behave the same way</i>! For me <i>behave the same way</i> means executing the same code using different
    property values. And there is no decision made based on a properties value (accept validation checks).

    In this sence the elements of an enum can be just parameterized instances of it or they can be instances of anonymous childs of the enum (changing the behavior by overriding methods of the enum).
    Enum class instances are meant to behave differently from other instances of the same class. If its not by interface, atleast with their cardinal order they should. Am I right?
    No. This is not <i>different behavior</i>.
    Or do you think the performence of a car depends on its color?

    bye
    TPD
  • 12. Re: Enum
    aksarben Journeyer
    Currently Being Moderated
    One particularly good use I've found for enum's is representing database codes. In our system, the codes are frequently cryptic letters & numbers, & their meaning isn't always obvious. Instead of using the codes directly in our app, we embed them in an enum (as a member variable, set via the constructor), & thus we can use the enum name in the Java code, making the meaning much more obvious, reducing the risk of human error, improving ease of maintenance, etc.

    Of course, this is only one use case.The more you use enum's & become familiar with them, the more you'll find other ways they can be helpful.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points