Results 1 to 3 of 3
  1. #1
    rsai is offline Member
    Join Date
    Jul 2012
    Posts
    1
    Rep Power
    0

    Default 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,

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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) {
    ...
    }
    You can pass the constant such as ONE, TWO as the param values. But you can also pass 3, 4, 5, etc as the value to the 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) {
    ...
    }

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: java enum : type safety

    Using enum will give you compile time checking of valid values
    The annoying Font(String,int,int) constructor being an example. Who remembers whether the arguments are name-style-size or name-size-style? The problem wouldn't arise if the style were properly typed as a Style rather than misusing int.

    -----

    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

  1. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  2. Creating axis2 webservice-Enum type problem
    By sertacyilmaz in forum Advanced Java
    Replies: 1
    Last Post: 09-13-2011, 11:51 AM
  3. public static enum vs enum class
    By Dipke in forum New To Java
    Replies: 3
    Last Post: 08-30-2011, 11:45 AM
  4. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 07:02 PM
  5. passing an enum type as a parameter ??!
    By SCS17 in forum New To Java
    Replies: 11
    Last Post: 07-13-2008, 02:44 PM

Tags for this Thread

Posting Permissions

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