Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 201
Alex Turbin Avatar asked Feb 12 '26 06:02

Alex Turbin


2 Answers

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.

like image 80
duffymo Avatar answered Feb 14 '26 20:02

duffymo


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"

like image 27
James Avatar answered Feb 14 '26 21:02

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!