• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pattern in java constant files

 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I saw two ways of coding a constant file.


Type 1:-
public class Constant{
public static final String STRING_NAME="jguru";
}
Type:2:-
public class Constant{
public static Constant name = new Constant("jguru");
}

Out of these 2 types which one will improve the
performance and why?


thanks
sri
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sriram sundararajan
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic