0

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?

9
  • 1
    Just a tip, if you're looking for suggestions for working code CodeReview SE would be a nice place to post Commented Jun 12, 2014 at 17:10
  • 2
    Why aren't you just calling it as GeofenceUnit.fill()? Commented Jun 12, 2014 at 17:11
  • if I remove the static modifiers from the GeofenceUnit class definition and its fill() method signature. Make sure you understand what it means to have a static nested class. Commented Jun 12, 2014 at 17:12
  • @JonSkeet I didn't know you could access the inner class that way. I thought you could only access inner class via this keyword. Commented Jun 12, 2014 at 17:14
  • This presents other problems because I do need to access member variables of the outer class from the inner class and I cannot do that with the static inner class. Commented Jun 12, 2014 at 17:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.