This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Mastering Corda: Blockchain for Java Developers and have Jamiel Sheikh on-line!
See this thread for details.
Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

properties v/s constants

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic