What is the best way to use labels and error messages? I definitely don't want to assign them inline (new Label("field1") but I've thought of the following choices:
1) private static final String class variables
2) public static final String variables in an Interface, then implement the interface where needed
3) Access via ResourceBundle.
OPT 1 - does not allow me to use in other classes, like JUnits
OPT 2 - seems to work best, but is it best-practices?
OPT 3 - These strings will not be translated, so is ResourceBundle overkill? I'd also rather not create a .properties file. Is OK to use ResourceBundle with a Java file of strings?
I find option 2 really ugly, especially when you implement the interface just to have the constants available in your class. That is, in my opinion, abusing inheritance. Inheritance is supposed to always indicate an "is a" relationship, and it's strange to say "my class X is a Messages" (where Messages is the interface with the constants).
If you use option 2, then at least do not implement the interface, but just use it like this:
If you don't want to type "Messages..." al the time, you could use static import to import the constants from the interface. From that page:
In order to get around this, people sometimes put static members into an interface and inherit from that interface. This is a bad idea. In fact, it's such a bad idea that there's a name for it: the Constant Interface Antipattern (see Effective Java Item 17). The problem is that a class's use of the static members of another class is a mere implementation detail. When a class implements an interface, it becomes part of the class's public API. Implementation details should not leak into public APIs.
if OPT 3 is an overkill, then hardcoding messages in your java file will prove maintenance nightmare. What if some messages has to be added or corrected and there is no developer around ? Properties file gives you less maintenance burden. This is not related your question directly. It was a design issue. You can use public static final very well.
Why do you want to avoid using a .properties file?
posted 10 years ago
I can use a properties bundle. But that raises other questions for me.
1) I still need somewhere to store all the keys that will be reused. Static strings in another class?
2) Accessing the strings from multiple classes - what is the best way to re-use or access a ResourceBundle? Is this a shared object? Does each class that uses the strings create it's own ResourceBundle?
Rahul P Kumar
posted 10 years ago
Make your properties reference variable static then. Once properties file loaded, behaves as hashmap. Since you need to access value across classes and those classes may have different keys (as i understood, elaborate your point otherwise), they will query this map with their key and get correct value. Since property file is not being modified by individual class, so it is safe to share among classes.
Yeah, but is it art? What do you think tiny ad?
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF