I am not clear on the following:
A class is loaded by JVM when needed, like lazy initialization, right?
Now if class A does an import of class B which class B actually is not in the file system (e.g. B.class was deleted or not delivered or any reason)
then does class A get loaded and runs if no method of class B is called?
Or class A is not able to run at all since the imports can not be resolved?
Or class A is loaded and run up to a certain point?
I am not clear on the following: A class is loaded by JVM when
Share
importstatement is only important for the compiler. In bytecode all references to other classes are fully qualified. That’s why superflous imports don’t matter at runtime.In your case JVM will try to load all classes that are required to load and verify
A, so it will try to load, but dependant classes are loaded lazily only when they are needed. Check out the following example:BimmediatelyCompile
A.javaand deleteB.class. Without callingbar()method your program will run just fine. But once you uncomment piece of code actually usingBclass you’ll get nasty:If
Bis not available, you’ll getNoClassDefFoundor similar.