Results 1 to 8 of 8
Thread: why we are using enums in Java?
- 01-05-2010, 10:27 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
- 01-05-2010, 10:28 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What do the books/tutorials say?
- 01-05-2010, 10:44 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
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.
- 01-05-2010, 11:35 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Enums are type safe constants; imagine ints like this:
and some other unrelated ints: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;
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.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;
kind regards,
Jos
- 01-05-2010, 12:46 PM #5gcampton Guest
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; } }
- 01-05-2010, 06:16 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Thank u very much josh and
gcampton . It really gave me good knowledge.:)
- 01-06-2010, 09:18 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
- 01-08-2010, 04:41 PM #8
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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.
I put this Status as private filed of my Transaction class.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; } }///
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 ;)


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks