I am having this issue I can't seem to resolve: I am trying to create a network of 'Location' objects. Each Location has a name and is connected to other 'adjacentLocations'. However, because it references adjacentLocations, I cannot initialize each Location Object fully because the adjacentLocations still need to be initialized.
I am coding in Java, a sample of what I mean is below:
public class Location{
private String name;
private Location[] adjacentLocations;
public Location(String name, Location[] adjacentLocations){
this.name = name;
this.adjacentLocations = adjacentLocations;
}
Location bar = new Location("bar",new Location[]{townSquare, dock});
Location townSquare = new Location("town square",new Location[]{dock, bar});
Location dock = new Location("dock", new Location[]{bar, townSquare});
}
I do not plan on accessing the contents of the adjacent Locations until the whole "map" is initialized.
I have tried putting the code in a main method, it complains that it cannot resolve symbol townSquare
.
I have tried declaring all the Locations and initializing within a main method, and it complains that townSquare may have not been initialized
.
I have tried the above code which complains illegal forward reference
I know I could try initializing all of my Locations with empty adjacent Locations and then adding them in post, but that seems like anything but elegant, and a pain in the butt. Am I missing something obvious? I tried looking up all of those error codes and did not find a useful solution to my problem.
You obviously can not construct object which depend on stuff that does not yet exist. What you have created is a chicken-and-egg problem thats hard to solve in any language.
A good choice would be to manage adjacency externally, e.g. through adjacency lists:
You can of couse also initialize the
adjacentLocations
empty and set them later, once those adjacent locations have indeed been constructed.You can hide the mechanism from callers by creating an interface:
With proper visibility rules and modules set up, no one can manipulate these objects further.