Java / JVM Architecture (Java21) Interview questions
What is the difference between Class.forName() and ClassLoader.loadClass()?
Both methods load a class dynamically by name, but they differ in how much of the class lifecycle they trigger and which loader they default to.
| Class.forName(name) | loader.loadClass(name) |
| Loads, links, and initializes the class by default | Only loads (and links); does not initialize |
| Uses the caller's own class loader by default | Uses whichever ClassLoader instance you call it on |
Has an overload forName(name, initialize, loader) to control both | No initialization control - callers must trigger it separately if needed |
This distinction matters in practice: JDBC drivers historically relied on Class.forName() specifically because it runs static initializers, which is how a driver class registers itself with DriverManager.
More Related questions...