Why use constants instead of enumerations?

I have seen many, many Java libraries using many constants where enumerations could be easily used. Even in Swing, there is a lot of code that uses constants instead of enums. Why?

What are the disadvantages of using enums?

+6
source share
4 answers
  • There is a lot of documentation on using preliminary solutions.
  • There are many developers who use Java for the first time and have experience working in another language, where using constants is the norm.
  • Many libraries wanted to support Java 1.4 and earlier. Java 1.3 and 1.4 are still in use today.
  • The working code has not been rewritten to use enumerations.

I find that I'm still telling people to use enum for singleton, although it has been around for 7 years .: P

When will use constants be used instead of enum? When you have a lot of related but not directly related constants.

public static final int BUFFER_SIZE = 32 * 1024; public static final String ERROR = "Error: "; public static final char TAB = 't'; 
+8
source

Since enumerations were introduced in Java 5, and these libraries were written long before that. Refactoring them will break the database of existing applications.

+12
source

Enumerations in java are introduced in Java 5, there were none before, and Enum is equivalent to a class in java.

+1
source

Prior to Java 1.5, Java did not have an enum , and the easiest way to implement an enum pattern was to use const int. You can read more in the Java Tutorial section for listings .

0
source

Source: https://habr.com/ru/post/894523/


All Articles