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);
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.
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
Re: General question, why don't Java classes use enums?
Quote:
Originally Posted by
Eranga
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
Re: General question, why don't Java classes use enums?
Quote:
Originally Posted by
JosAH
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...
Re: General question, why don't Java classes use enums?
Quote:
Originally Posted by
Eranga
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
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?
Re: General question, why don't Java classes use enums?
Quote:
Originally Posted by
Anza Power
^ 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.
Re: General question, why don't Java classes use enums?
Quote:
Originally Posted by
JosAH
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.
Re: General question, why don't Java classes use enums?
Quote:
Originally Posted by
Anza Power
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.