Hey, I have a CS problem that I'm trying to handle. It revolves around the best way to handle a large (200ish) number of "final" Strings, and how best to store/call them for use.
The question, simply, is what is the best way to store/call them? Currently I have them all in a static class from which I simply say ClassName.StringName to call these strings. This is functional, but I am curious to know if there's a better way to be going about this.
If I'm not being clear, I'll give an example:
Say I have 4 strings named "Thing 1", "Thing 2", "Thing 3", and "Thing 4", and I needed to call upon these strings in different classes, like this:
class foo
{
private String localThingOne = "Thing 1";
private String localThingTwo = "Thing 2";
private String localThingThree = "Thing 3";
}
class bar
{
private String localThingOne = "Thing 1";
private String localThingTwo = "Thing 2";
private String localThingThree = "Thing 3";
}
Good programming practice tells me that if I'm going to use the same String multiple times like this, I ought to store this string in a single variable and call upon the variable, in case that string were to need changing. So, I have created a class called "Things", here:
class Thing
{
public static Final String ThingOne = "Thing 1";
public static Final String ThingOne = "Thing 2";
public static Final String ThingOne = "Thing 3";
}
and in my other classes I now do this:
class foo
{
private String localThingOne = Thing.ThingOne;
private String localThingTwo = Thing.ThingTwo;
private String localThingThree = Thing.ThingThree;
}
class bar
{
private String localThingOne = Thing.ThingOne;
private String localThingTwo = Thing.ThingTwo;
private String localThingThree = Thing.ThingThree;
}
Additional info. pertaining to my specific situation:
- All of the classes that will be calling these Strings are going to inherit the same parent class, so I could store them in the parent class as public Strings and call upon them via inheritance.
- I have about 200 Strings, and these Strings change frequently, so being able to change them universally is a must.
- I have access to a database, and I could store them in this database, and call upon them through a SQL query.
So to restate my question, is there a better way to do this?
EDIT:
To add one more thing, each String almost needs to be identifiable when it is called. What I mean is, Thing.ThingOne works, wheras Thing.Things[2] is not descriptive enough and will lead to incredible confusion in the specific problem :-)