Consider the following two Java classes:
a.) class Test { void foo(Object foobar) { } }
b.) class Test { void foo(pkg.not.in.classpath.FooBar foobar) { } }
It would be a violation of the JLS to compile a class without looking at the type signatures of the classes it depends on. No conformant Java compiler would allow you to do this.
However ... it is possible to do something rather similar. Specifically, if we have a class A and a class B that depends on A, it is possible to do the following:
If you do this, the application will fail with a IncompatibleClassChangeError
when the class loader notices the signature incompatibility.
Actually, this illustrates why compiling ignoring dependencies would be a bad idea. If you run an application with inconsistent bytecode files, (only) the first inconsistency detected will be reported. So if you have lots of inconsistencies, you will need to run your application lots of times to "detect" them all. Indeed, if there is any dynamic loading of classes (e.g. using Class.forName()
) in the application or any of its dependencies, then some of these problems may not show up immediately.
In summary, the cost of ignoring dependencies at compile time would be slower Java development and less reliable Java applications.