简体   繁体   中英

Class Loading: Compile time or Runtime

Is java class loaded at compile time or Runtime? I know you can dynamically load a java class using your own Class Loaders or calling Class.forName("fully qualified name of java file") .

But if I am not using any of the above techniques then does it means that Java classes are loaded at compile time?

No. The JLS explains how loading and linking is runtime. Any binary-compatible revision of a class can be thrown in place of an existing class.

A class is compiled with a symbolic reference to other classes, not the code of those classes.

However with certain non-JVM compilers like GCJ classes can be compiled ahead of time into one executable.

Classes are loaded at runtime to execute their code.

Classes are loaded at compile time to check code using the class for type safety.
Whenever you write code that uses a class (eg, calling a method on it), the compiler needs to load that class to make sense of your code (eg, to check methods or base type)

At compile time nothing is loaded. At compile time classes are just generated from sources.

The difference could be if a class is loaded by the ClassLoader when the JVM powers up or if you do it dynamically during the execution, but they are two sides of the same medal.

In both cases they are loaded dynamically, but in the former this is done as soon as the JVM starts.

As many others have stated, classes are loaded at runtime unless you are using an AOT compiler to allow them to run in non-JVM environments. If you want to read up all the details on how the Java Virtual Machine starts up, initializes and loads the first then subsequent classes you can take a look at the Java SE 7 specifications .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM