Results 1 to 8 of 8
  1. #1
    manish.anchan is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default why we are using enums in Java?

    what is the use of enum and when it can be more useful?:confused:

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What do the books/tutorials say?

  3. #3
    manish.anchan is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default RE:

    Actually in tutorial they have mentioned that when ever we need fixed set of constants we can apply it. But my query is when arrays are there, why do we need that. what makes it more essential.
    Last edited by manish.anchan; 01-05-2010 at 11:03 AM.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by manish.anchan View Post
    what is the use of enum and when it can be more useful?:confused:
    Enums are type safe constants; imagine ints like this:

    Java Code:
    // in one class. say BorderLayout:
    public static final int NORTH= 0;
    public static final int EAST= 1;
    public static final int SOUTH= 2;
    public static final int WEST= 3;
    and some other unrelated ints:

    Java Code:
    // in you class, modeling a card:
    public static final int SPADES= 0;
    public static final int HEARTS= 1;
    public static final int DIAMONDS= 2;
    public static final int CLUBS= 3;
    There is nothing that prevents you from using an int from the first class and use it in your second class. Your code will be a mess before you know it but the compiler cannot detect the (ab)use of those ints. Two separate Enums forbid this madness.

    kind regards,

    Jos

  5. #5
    gcampton Guest

    Default

    And for some more random uses...
    Java Code:
    enum Apple { 
      A(10), B(9), C, D(15), E(8); 
     
      private int price; // price of each apple 
     
      // Constructor 
      Apple(int p) { price = p; } 
     
      // Overloaded constructor 
      Apple() { price = -1; } 
     
      int getPrice() { return price; } 
    }

  6. #6
    manish.anchan is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank u very much josh and
    gcampton . It really gave me good knowledge.:)

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

  8. #8
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Here is why enums are beeter than classic java constants:

    Java: Enums

    Think of it as special class definition
    which gives you power to represent set of data in easy manner
    and control and use is it easily.

    JosAH made good point about enforcing type safety in your code.
    Just define your special class as enum, add some entries
    and use it it some other class as field.

    I use it often to represent status of some transaction in my sistem.


    Java Code:
    //---enumerator for Transaction status .
    	public enum Status{
    
    		ER1_TR_OK	(10,"Transaction OK"),
    		ER2_LIMIT	(16,"Over transaction limit."),
    		ER3_INTERNAL	(21,"Internal error");
    
    		private int errorCode;
    		private String errorDesc;
    
    		private Status (int i, String s){
    			this.errorCode = i;
    			this.errorDesc = s;
    			
    		}
    
    	}///
    I put this Status as private filed of my Transaction class.

    Now, user can never set Transaction Status
    on some, lets say integer value,
    that is not defined in enum above,
    user must use enum!

    Also representation is very clear to him
    because of descriptive name "ER1_TR_OK"
    not just some int value that is mapped somewhere in code
    to something more meaningful.


    hope this will clear things up for you ;)

Posting Permissions

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