Sharon Watinsan
Sharon Watinsan

Reputation: 9850

Having String messages in a different class

I am going to have a lot of String text in my application. Therefore i thought to have all these Strings in 1 class. So that it would be easy to go and modify a String if there's a requirement to.

public class MyMessage {

static final String NAME = "JOE";
static final String AGE = "21";
......

}

And then access it in other classes like MyMessage.NAME and MyMessage.AGE.

1.) Is this approach correct ?

2.) I also found that, we could use property files to do so. Is this true? (And what is the best approach to do this property file or having static methods in a java class)

Upvotes: 0

Views: 231

Answers (4)

invariant
invariant

Reputation: 8900

@invariant Other's disagree with my approach. What are your comments on this ?

well it depends on how often you change those values ,Let me explain with one example,In my application i have to work with state names of US in many classes for this purpose i prefer to create a class

public class Juridictions {
public static final String OH = "OH";
public static final String CA = "CA";
..

}
   //when ever i need
   if(localvalue.equals(Juridictions.CA) {
   system.out.println("you are from CA !");
}

using properties file approach for this use case(i believe state names don't change!) is Premature Optimization (because it requires more code to read from file then using values when ever you need than above approach)

If you're not sure about how often you change your values , then follow when in doubt leave it out ,

Upvotes: 1

jbx
jbx

Reputation: 22158

It depends a little what you really want to achieve. Do you want it to be easy for you to add new strings without modifying the source code of the program? Do you want to support multiple languages? It depends what your actual situation is.

Apart from what you suggested, a popular approach is to use Resource Bundles. These are essentially used to group strings and make it easier for you to edit them. Another feature they support (you might not need this) is having multiple languages, i.e. a different resource bundle for each language you want to support. The actual strings are held in property files.

This is a good tutorial: http://www.avajava.com/tutorials/lessons/how-do-i-read-a-properties-file-with-a-resource-bundle.html

Upvotes: 1

rai.skumar
rai.skumar

Reputation: 10677

If you want to change value of any constant in your constant class file you will have to recompile the class. But you don't need to do it when you using properties file. So properties file is better option.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1501163

Yes, you can use property files, and typically this is a better approach. Aside from anything else, this allows you to pick up different resources for different languages. See the documentation for ResourceBundle and PropertyResourceBundle for more information.

You may still want to have a type to maintain the resource bundle keys, but personally I'd make that an enum rather than constants in a class.

Upvotes: 3

Related Questions