Results 1 to 10 of 10
  1. #1
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default General question, why don't Java classes use enums?

    For example alot of classes in Swing use static final integers like BorderLayout.NORTH, JFrame.EXIT_ON_CLOSE, why aren't they defined as enums? if they were enums it would be safer because you can detect errors like these during compile time:

    SomeObject.setDirection(OtherObject.SOMETHIONG_UNR ELATED);

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

    Default Re: General question, why don't Java classes use enums?

    I would guess that they predate enums. You could check the Enum API docs to see when they became available.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: General question, why don't Java classes use enums?

    I agreed because of two reasons,

    1. Enums are much type-safe than integer and so on
    2. They make codes more readable

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: General question, why don't Java classes use enums?

    Quote Originally Posted by Eranga View Post
    I agreed because of two reasons,

    1. Enums are much type-safe than integer and so on
    2. They make codes more readable
    Changing all those magic numbers to enums would be a noble deed indeed, but it would break a lot of existing code ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: General question, why don't Java classes use enums?

    Quote Originally Posted by JosAH View Post
    Changing all those magic numbers to enums would be a noble deed indeed, but it would break a lot of existing code ...

    kind regards,

    Jos
    Of course. I like to make my code more and more readable...

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: General question, why don't Java classes use enums?

    Quote Originally Posted by Eranga View Post
    Of course. I like to make my code more and more readable...
    If you do it well and use those symbolic (int) constants and don't rely on the fact that they are ints, enums won't buy you much w.r.t. readablilty.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Anza Power's Avatar
    Anza Power is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: General question, why don't Java classes use enums?

    ^ The most important thing is type-safty, some constants can be really confusing especially when they don't belong to the same class (like TOP vs NORTH)

    Couldn't they just deprecate those methods (but keep them defined) and add copies of each method that uses enums?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: General question, why don't Java classes use enums?

    Quote Originally Posted by Anza Power View Post
    ^ The most important thing is type-safty, some constants can be really confusing especially when they don't belong to the same class (like TOP vs NORTH)

    Couldn't they just deprecate those methods (but keep them defined) and add copies of each method that uses enums?
    There are methods and then the values passed into those methods, and they're usually ints or Strings. In order to not break backwards compatibility, they'd have to add an enum version of each constant, as well as a new method for every method that accepts one of those values. I'd rather them spend their time on other stuff, as this is a non-issue that doesn't actually affect development.

    If it bothers you so much, you could define your own enums that each contain a method that returns the corresponding value.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: General question, why don't Java classes use enums?

    Quote Originally Posted by JosAH View Post
    If you do it well and use those symbolic (int) constants and don't rely on the fact that they are ints, enums won't buy you much w.r.t. readablilty.

    kind regards,

    Jos
    I like to work with symbols a lot (may be because of C/C++ background). It gives me a more readable.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default Re: General question, why don't Java classes use enums?

    Quote Originally Posted by Anza Power View Post
    some constants can be really confusing especially when they don't belong to the same class (like TOP vs NORTH)
    Because of the way we named them.

Similar Threads

  1. Replies: 0
    Last Post: 10-14-2011, 05:16 PM
  2. A general question
    By wizar in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 11:13 PM
  3. General Question about Java
    By bayan in forum New To Java
    Replies: 6
    Last Post: 05-26-2010, 12:16 PM
  4. Replies: 9
    Last Post: 12-01-2009, 09:30 PM
  5. general confused about java question (easy!)
    By sweetjava in forum New To Java
    Replies: 1
    Last Post: 08-09-2009, 03:03 AM

Posting Permissions

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