Operator
import important only to the compiler. In the bytecode, all references to other classes are fully qualified. That super-dense imports do not matter at runtime.
In your case, the JVM will try to load all the classes that need to be loaded and check A so he will try to load B immediately , but dependent classes are loaded lazily only when they are needed. See the following example:
public class A { public static void bar() { new B().foo(); } public static void main(String[] args) {
Compile A.java and uninstall B.class . Without calling the bar() method, your program will work fine. But as soon as you uncomment the part of the code using class B , you will be disgusted:
Exception in thread "main" java.lang.NoClassDefFoundError: B at A.bar(A.java:4) at A.main(A.java:8) Caused by: java.lang.ClassNotFoundException: B at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 2 more
If B not available, you will get NoClassDefFound or the like.
source share