I ran across an exception today and managed to eliminate the error. At first, let me give you an example code.
public class Foo () {
protected String var;
}
public class Bar extends Foo () {
public String isVarNull() {
return (this.var == null);
}
}
So these are my two example classes. Both classes are located in some jar. Everything works fine in this scenario (imageine they were usefull classes and are used in some productive enviroment).
All I do now is change the type of Var
in Foo
to Integer
.
public class Foo () {
protected Integer var;
}
Afterwards I compiled class Foo
and replaced the old Class-File in the Jar with the new one. When I try to access the field var
now (in method isVarNull()
of class Bar
) I get this Exception:
Caused by: java.lang.NoSuchFieldError: test/Bar.var at test.Bar.isVarNull(Bar.java:6)
As far as I believe it has something to do with the bytecode of the class Bar
. Do compiled classes "know" the type of a method/variable used in the code? In this case, does Bar
"know", what type var
should return and therefore throw an error, because the returning type of the method has changed?
I would really appreciate a detailed answer!
Kind regards