aspose file tools*
The moose likes Java in General and the fly likes properties v/s constants 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 "properties v/s constants" Watch "properties v/s constants" New topic
Author

properties v/s constants

aparna chi
Greenhorn

Joined: May 19, 2004
Posts: 16
Consider the scenario that you have some strings in your application. Whose value may change some time in future.

What would be the best possible solution for this?
Using properties file and loading them through resource bundle
or
Using a java class which contains public static final strings for the changing values.

Which one is better and why?

Thanks
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I'm not sure I completely understand what's going on...however, the String class is immutable. You can't modify a String (if you try to, it creates an entirely new object and leaves the original unchanged). Of course, depending on what's going on, your references might be changing and pointing to different objects...
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
i would go for properties files and ResourceBundles. You can send these files to editors/translators, can pack different versions of it with different releases and can also provide several languages with your app.

pascal
aparna chi
Greenhorn

Joined: May 19, 2004
Posts: 16
We have some string constants like key1 = "value" in our system. These constants are used in multiple places throughout system using the key e.g. key1 here.

There are two ways of accessing these
1) I make a properties file constants.properties in which i put the values as key1 = value. And then access them through resource bundle.

2) I make a java class like:
public class ConstantsClass
{
public static final String key1 = "value";
}
and now access them as ConstantsClass.key1 wherever i need them.

So I want to know the which is best solution 1 or 2. I am saying that 1 is better and some are saying 2 is better. Their argument is that it will be resource intensive to have a properties file. I am saying that since it is web application which will utimately deployed as an ear , if we use a constants class we will have to rebuild and redeploy the whole application even for some small change.

I basically want to know the concrete differences between two apporaches and their effect on application. And which one to use in which case. Basically what's the better way and why in such situation.

Thanks for the replies.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The approach to take, as usual, depends on how these strings are being used by your application. Your only constraints are that it's a web application and the strings "may change some time in [the] future." The frequency of change would be first priority to me.

If you want to change them at runtime and see the results immediately (e.g. queries or display formats), you can externalize them (properties files, resource bundles, database, etc), but you'll also need methods to reload them dynamically without redploying. If redploying is okay, it's far easier.

Keep in mind that if you externalize them, you still need a compile-time constant with which to find the real value. Consider a date format string, originally as a constant.If you put it into a properties file,you will now need to put its key into a constant so you can access its value.And of course remember the YAGNI principle. Are you so sure up front that the values will change frequently enough to warrent the extra development effort? Of course, if you hide both access methods behind a facade, you won't have to change client code when you change storage methods.can be replaced with the following without changing any code that calls it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: properties v/s constants