I have a public Unit class. I only want to access GeofenceUnit class THROUGH Unit class. Thus, I make GeofenceUnit an inner class of Unit. A Unit has many GeofenceUnits. Consequently, when I instantiate a Unit, I want to store many GeofenceUnits in the Unit object.
public class Unit {
public ArrayList<Unit.GeofenceUnit> geofences;
public Unit(int id){
this.id = id;
geofences = this.geofence.fill();
}
private static class GeofenceUnit {
ArrayList<GeofenceUnit> geofences;
private static ArrayList<Unit.GeofenceUnit> fill(){
...
while(resultSet.next()){
geofences.add(new Geofence());
}
return geofences;
}
}
}
The problem with the above code, as you may have noticed, is I am trying to call the static method fill() within the constructor of Unit. This yields the warning "The static method fill() from the type Unit.GeofenceUnit should be accessed in a static way". And I absolutely agree with the warning. I don't want to have to access it through a static way. However, if I remove the static modifiers from the GeofenceUnit class definition and its fill() method signature, then it doesn't logically make sense. Why would I populate many GeofenceUnits in an instance method. Good program practice suggest that method should be made static.
I think I just have a bad design right here. Any suggestions on how to refactor this?