Does JVM loads all used classes when loading a particular class?

梦想与她 提交于 2019-12-09 23:37:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!