Sometimes I need to test which class has declared some variable(s), is there another way how to test that, if concrete class contains variable with some name
try {
testLocalVariable = (String) (this.getClass().getDeclaredField("testVariable").get(this));
} catch (NoSuchFieldException ex) {
} catch (SecurityException ex) {
} catch (IllegalArgumentException ex) {
} catch (IllegalAccessException ex) {
}
If I understand correctly, you use this code in a superclass to test if a subclass has a
testVariablefield.Why don’t you simply add a method like this?
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.