Results 1 to 3 of 3
Thread: java enum : type safety
- 07-05-2012, 03:54 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
java enum : type safety
Hello,
I am trying to understand the importance of Java Enum and wondering what value is the java enum is bringing in?
I mean prior to 1.5, we used to use:
public static int ONE = 1;
public static int TWO = 1;
Now with enum, we are defining differently
public class enum{
ONE (1), TWO(2);
}
Not very clear as to what type safety info is the enum having over the previous version and the other advantages.
Thanks in advance,
- 07-05-2012, 04:26 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: java enum : type safety
Using enum will give you compile time checking of valid values. From the example you give above let say that you have a method like this:
Java Code:public void doSomething(int param) { ... }
If you change the param to use enum then you can ensure that the param can only accept the value of ONE or TWO of your enum. No other value will be accepted as the param.
Java Code:public void doSomething(MyEnum param) { ... }
Website: Learn Java by Examples
- 07-05-2012, 04:45 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: java enum : type safety
Using enum will give you compile time checking of valid values
-----
It strikes me that Google is a better place to apply for an answer to this question (and its ilk). The introduction of enums was accompanied at the time with much fanfare and blogging which remain, to this day, as rounded, considered answers to the question. If something found there is confusing or strikes you as wrong, ask a particular question about that.
Similar Threads
-
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 01:49 PM -
Creating axis2 webservice-Enum type problem
By sertacyilmaz in forum Advanced JavaReplies: 1Last Post: 09-13-2011, 11:51 AM -
public static enum vs enum class
By Dipke in forum New To JavaReplies: 3Last Post: 08-30-2011, 11:45 AM -
Setting values from One Enum type to another enum type.
By reach2sudhakar in forum New To JavaReplies: 3Last Post: 09-23-2010, 07:02 PM -
passing an enum type as a parameter ??!
By SCS17 in forum New To JavaReplies: 11Last Post: 07-13-2008, 02:44 PM
Bookmarks