This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Actually, most people refer to these types of things as "enums" rather than "constants." And, I don't know if either one is faster than the other. However, the second method is more strongly typed. Consider a method that takes one of your Constant objects vs. one that takes a String. For the method that takes a String, you can pass in ANY string. However, if you use an "enum class" like Constant, then you can be sure that they're passing in a valid constant.
James Carman, President<br />Carman Consulting, Inc.
The second variant - the Typesafe Enum pattern - also has the advantage that you can add behaviour to it. It's similar to the new enum feature in Java 5, and definitely prefered over the usage of simple Strings/ints/etc.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Apr 08, 2003
Hi, Thanks for your tips, when i was referring the type safe enum pattern i happend to read this article in javaworld http://www.javaworld.com/javaworld/javatips/jw-javatip122.html i got confused, what is the significance of method readResolve() and in which class it has been defined. I got one more doubt, for the constant why should i spend so much of time by creating this Enum class and what am going to achieve by following this pattern. Is it going to improve my performance by any chance?
The reasons to use enum classes include readability and maintainability by imparting knowledge about the use of the enum as well as type safety (ensuring that someone doesn't pass you an invalid enum). Performance will be impacted slightly negatively, but it's the general concensus that the tiny cost is well worth the many benefits.
Joined: Jul 11, 2001
Originally posted by sriram sundararajan: what is the significance of method readResolve() and in which class it has been defined.
This is only significant if the enum needs to be Serializable and you want to compare its instances via "==" instead of equals. It's likely that you can ignore it for now.
I got one more doubt, for the constant why should i spend so much of time by creating this Enum class and what am going to achieve by following this pattern. Is it going to improve my performance by any chance?
Performance is the last thing a good designer thinks about - maintainability is much more important.
Besides being typesafe and increasing readability, it the typesafe enum pattern also has the big advantage of allowing you to move behaviour into the enum itself. That way you can replace switch and if statements by polymorphic method calls. This becomes very handy when you need to introduce a new enum value - instead of having to adjust all the if and switch statements all over your code, you just implement the correct behaviour inside the enum itself, and all works fine!