aspose file tools*
The moose likes Java in General and the fly likes pattern in java constant files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "pattern in java constant files" Watch "pattern in java constant files" New topic
Author

pattern in java constant files

sriram sundararajan
Ranch Hand

Joined: Apr 08, 2003
Posts: 43
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

Joined: Feb 20, 2001
Posts: 580
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
sriram sundararajan
Ranch Hand

Joined: Apr 08, 2003
Posts: 43
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

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Jul 11, 2001
Posts: 14112
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!
 
 
subject: pattern in java constant files