问题
When JVM loads a class A, does it load all of the classes used within A?
And I'm wondering if import declarations are matter somehow to the loading process?
The link to JLS would be appreciated.
回答1:
Import and class loading are unrelated. The former just saves typing: it allows you to use the short class name rather than the fully-resolved class name in your code.
Classes are loaded by the JVM when they're used for the first time.
回答2:
import
merely helps the programmer. When the class file is compiled the Qualified Name of the variables is stored in the .class file so the JVM knows what it needs to load.
http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#21410 section 2.17.1 "Virtual Machine Start-up"
回答3:
Importing a class is marker for the user and the compiler. Using import does not load classes. JVM loads class only when it referenced and if the classes are not required then the class does not load it on compile time or runtime.
来源:https://stackoverflow.com/questions/5702423/does-jvm-loads-all-used-classes-when-loading-a-particular-class