Results 1 to 10 of 10
- 12-21-2011, 10:24 AM #1
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);
- 12-21-2011, 10:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 12-21-2011, 01:56 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
- 12-21-2011, 02:02 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: General question, why don't Java classes use enums?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-21-2011, 02:06 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: General question, why don't Java classes use enums?
- 12-21-2011, 02:38 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: General question, why don't Java classes use enums?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-21-2011, 07:10 PM #7
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?
- 12-21-2011, 07:16 PM #8
Re: General question, why don't Java classes use 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!
- 12-22-2011, 05:49 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: General question, why don't Java classes use enums?
- 12-22-2011, 05:50 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: General question, why don't Java classes use enums?
Similar Threads
-
javax.sound.midi.spi.MidiFileReader - and abstract classes in general
By viomartyr in forum New To JavaReplies: 0Last Post: 10-14-2011, 04:16 PM -
A general question
By wizar in forum New To JavaReplies: 6Last Post: 01-15-2011, 10:13 PM -
General Question about Java
By bayan in forum New To JavaReplies: 6Last Post: 05-26-2010, 11:16 AM -
A few general questions regarding scope of local inner classes.
By Half_Duplex in forum New To JavaReplies: 9Last Post: 12-01-2009, 08:30 PM -
general confused about java question (easy!)
By sweetjava in forum New To JavaReplies: 1Last Post: 08-09-2009, 02:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks