i just wonder about what a .class file depends on. For example, if in a
classpath of many files, I change one .java source file. Is it enough to
compile only this source file? Or will there be changes in other
dependent .class files as well?
There might be. It depends if the change that was made affects the
signature of the class it was in, or only the implementation. By
'signature', i mean:
- Whether the type is a class or an interface
- Whether the class is final or not
- Whether the class is abstract or not
- The superclass of the class
- The set of interfaces implemented by the class
- The names of any methods defined by the class
- The return types of any methods defined by the class
- The parameter types of any methods defined by the class
- The values of any public compile-time constant expressions (see JLS
section 15.28, but basically, public constants of primitive or string type
with definitions that can be resolved by the compiler)
And probably a few other things that don't spring to mind right now.
In some cases, changes to these will break other classes - obviously,
renaming a method called from another class will cause that class to
break. In others, the other class is still okay, but the bytecode
generated for it will change - the old bytecode won't work, the source
code will compile, and the new source code will work. For instance:
public class A {
public static void print(int x) {
System.out.println(x);
}
}
public class B {
public static void main(String... args) {
A.print(23);
}
}
If you change 'int' to 'long' in A, an attempt to run B will bomb out with
a NoSuchMethodError. Recompiling B will fix this.
When constants are involved, there are even cases where the old code will
run but do the wrong thing:
public class A {
public static final int X = 23;
}
public class B {
public static void main(String... args) {
System.out.println(A.X);
}
}
If you change A.X to 17, recompile A, and run B, it will still print 23.
tom