Notices
Results 1 to 3 of 3

Thread: How should handle a "large" amount of static final

  1. #1 How should handle a "large" amount of static final 
    New Member
    Join Date
    Oct 2008
    Posts
    2
    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 :-)


    Reply With Quote  
     

  2.  
     

  3. #2  
    New Member
    Join Date
    Oct 2008
    Posts
    2
    Alright, so I actually solved my own problem (imagine that!) by using enums!

    Rather than go through a full-on explanation, I'll just show how I did it:
    Code:
    class Thing
    {
         enum Foo
         {
              ThingOne("Thing1"),
              ThingTwo("Thing2"),
              ThingThree(Thing3");
    
              private String thingID;
              String getThing() { return thingID; }
              Thing(String thingID ) { this.thingID = thingID; }    	
    }
    And in the derived class all I would have to do is this to get ahold of my string:

    Code:
    class Stuff extends Thing
    {
         System.out.println(Foo.getThing(ThingOne));
    }
    Or something like that (exact syntax may be off).

    I hope this helps someone, somewhere. It helped me :-)


    Reply With Quote  
     

  4. #3  
    Forum Freshman
    Join Date
    Oct 2008
    Posts
    33
    Is it using fortun language?
    Reply With Quote  
     

Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •