Reputation: 3928
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?
Upvotes: 0
Views: 607
Reputation: 96385
When you say in the constructor
geofences = this.geofence.fill();
that is saying that the class Unit has an instance member variable called geofence, which is not true here. (It's not part of the code sample shown, anyway.)
Also since the method you want to call is static no instance needs to be involved (and it is better style to avoid calling static methods on object instances). In order to call your static method fill() on GeofenceUnit you should change the line to
geofences = Unit.GeofenceUnit.fill();
If the method is unrelated to an instance of the class then it makes sense for the method to be static. But having a static method make a database call is ugly, it makes it hard to mock the results from the query, and entangles business logic with infrastructure code.
When you make an inner class static that means there is no connection between the outer class and it, the inner class does not have access to the outer class. So the static inner class can be instantiated independently of an outer class instance, but it doesn't have any special access to any instance of the outer class either. I'm not sure it makes sense to have this be a static inner class, or even an inner class.
I do not think you are making your life any easier by making JDBC calls in the constructor, or by putting the database access in a static method. The approach seems too concerned with privacy and not concerned enough with the single-reponsibility principle or easy unit testing.
An alternative would be to have domain objects that do not know how they are populated, with separate data access objects (otherwise known as repositories) to retrieve the data from the database and populate the domain objects.
Upvotes: 1
Reputation: 1454
The 'this' modifier implies that the GeofenceUnit you are referring should be istantiated, which is impossibile since its static. You should access to the static method usong directly the class as Jhon Skeet says: GeofenceUnit.fill()
Upvotes: 1