Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

best way to re-use strings (labels and error messages)

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keri:

Why do you want to avoid using a .properties file?

John.
 
Keri Mathis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic