Test if a class contains an instance variable based on its name

If I understand correctly, you use this code in a superclass to test if a subclass has a testVariable field.

Why don’t you simply add a method like this?

 /**
  * Returns true if the object declares a testVariable field, false otherwise. Subclasses should
  * override this method
  */
protected boolean hasTestVariableField() {
    return false;
}

Seems much more OO to me, doesn’t break encapsulation.

That said, I’ve not really understood why you needed this in the first place.

Leave a Comment