Search code examples
javastatictreemap

Two classes sharing static variable in Java


I have two classes, class Place and class Game. This is not an inheritance relationship but I want my Game class to have a Place treemap and my Place class to also have a treemap but I want both treemaps to be one in the same thing so that I don't have to constantly update both.

The other issue that arose was due to the nature of what I need to do, whenever I create a place object in the constructor, I need to immediately put the object in the treemap with places.put(id, this), and the issue is that the constructor requires that I initialize my treemap, but if I initialize it every time the constructor is called, obviously I'll be getting new maps every time.

In short, I need a way to have two classes share the same static tree map and 2. just initialize it once so that I don't re-initialize it. I was thinking of doing something funky like having a boolean isSet data member but I don't want to go that route.

public Place(Scanner fileInput, Report rep) {
    // this constructor will only read enough information for one
    // place object and stop on the line for which place it read
    name = "";
    description = "";

    // report object contains fields to initialize to help
    // validate the data. // see report class
    int idNumber; 
    int linesToRead; // number of lines to read for one place object
    while ( (fileInput.hasNextLine() ) && rep.placeInfoFound() == false  ) {
    //1. Call getline. getline has a while loop that will continue
    // until it reaches a line of data worth reading
    // first data we expect is the places line
    // after that, we will be on the 'PLACES line which will have
    // two fields, both place and id.
    //2 call ListToTokens which will make an arraylist of words
    // where each element is a token. token sub 1 is the number of
    // places.

        ArrayList<String> tokens = new ArrayList<String>();
        tokens = findPlaceInfo(fileInput, rep);
    }
    places = new TreeMap<Integer, Place>();
    places.put(id, this);
    //WE HAVE TO REINITIALIZE the fact that we found
    // place info back to false so that when we call again
    // we can run the while loop
    rep.assert_place_info_found(false);
} // function brace

private static TreeMap<Integer, Place>places;

Solution

  • You want to make the map a static member of the Place class (or the Game class, it does not matter which) like follows:

    import java.util.Map;
    import java.util.TreeMap;
    
    public class Place {
        //  Static Attributes
        private static final Map<Place, Object> places = new TreeMap<>();
    
        //  Constructors
        public Place(final Object key, final Place value) {
            places.put(key, value);
        }
    
        //  Methods - Static Getters
        public static Map<Place, Object> getPlaces() { return places; }
    }
    

    So, from your 'Game' class, you can call the Place::getPlaces function statically. If you have not yet made the 'Place' class, this will load the class and instantiate the static object. Then, you can cache that reference if you would care to do so.

    That being said, I would advise against structuring your code to rely on statics like that, as that tight coupling can lead to more complicated code that jumps all over the place. Though, I cannot make too many more suggestions without knowing your project and its requirements better, so I will leave it at that.

    Good luck!