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)
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
ResourceBundleandPropertyResourceBundlefor 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.