Using the operator assertin combination with the switch -ea, you can verify that the field name remains the same.
Programming With Assertions, , :
, , , . , . , , , , .
, .
: , , , :
public class Main {
int c = 5;
public static void main(String[] args) {
Field f = null;
try {
f = ReflectionTest.class.getDeclaredField("c");
} catch (NoSuchFieldException | SecurityException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
return;
}
assert hasMember(f);
System.out.println("We reached this point");
}
private static boolean hasMember(Field f) {
for (Field localField : Main.class.getDeclaredFields()) {
if (localField.getName().equals(f.getName())) {
return true;
}
}
return false;
}
}
class ReflectionTest {
int c = 10;
}
int c = 5; " ", int a = 5; :
Exception in thread "main" java.lang.AssertionError
at Main.main(Main.java:38)