This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
surya.raaj prakash wrote:Hi Friends,
what is difference between constant interface pattern and enum type? and which is better solution for declaring constants?
One reason enum was introduced in Java in the first place was to avoid the Constant Interface pattern. One could say that the "prefer composition over inheritance" principle was followed. The Constant Interface pattern is based on that you inherit a bunch of constants. That's not how enums are used. They cannot even be inherited.
The constant interface pattern is in fact an anti-pattern that should not be used. A final class with a private constructor is much better; java.sql.Types is an example.
An enum should be preferred because it also limits the number of values; using constants ints or Strings allows you to pass any int / String and you need to validate any arguments. Using an enum the only invalid value could be null. Only if you need to store constants of different types or do not care about invalid values should you consider using the constant class pattern these days.