You can use resource bundles. Create a file called messages.properties
in src
or the subdirectory corresponding to the desired package and add strings to it in the following manner. No quoting is needed.
ClassThatUsesAString.stringName=foo
AnotherClass.stringName=bar
AnotherClass.anotherString=baz
You can then access these strings as follows:
ResourceBundle rsrc = ResourceBundle.getBundle("messages");
and then:
rsrc.getString(stringName);
For example, if FooClass
requires string quxString
, then add to messages.properties:
FooClass.quxString=waldo was here
Notice lack of quotes. Now grab it with:
rsrc.getString("FooClass.quxString");
This does not enforce class boundaries and naming with the class is only for convenience.
If the strings are related closely, an enum may be better, either directly, or specifying message names for different localizations(i.e. an enum contains "ECONNREFUSED" and "ECONNABORTED" and those map to "Connection refused" and "Connection aborted" in one bundle and "conexión rechazada"/"Conexión abortada" in another bundle
Eclipse users: Eclipse does this automagically (though in a more complex fashion) with Source→Externalize Strings