JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
Class Loading Followed by Class Initialization
This section provides a tutorial example showing that class initialization happens immediately after class loading.
When a class is loaded into the JVM, the class will be initialized immediately. The initialization process includes allocating static variables and creating objects used by those static variables.
In order to create objects for those static variables, other classes will be loaded too.
To verify this behavior, I wrote 3 classes:
Client.java - The primary class:
class Client { String name; static Address home = new Address(); static Account checking = null; }
Address.java - A supporting class for Client.java:
class Address { String street; String city; String country; }
Account.java: Another supporting class for Client.java:
class Account { String status; double balance; }
Let's compile all classes, then load the "Client" class. I see no problem.
herong> javac Address.java herong> javac Account.java herong> javac Client.java herong> java -classpath . ClassCheck Client Class: Client Loader: jdk.internal.loader.ClassLoaders$AppClassLoader@1ff8b8f Loaded from: file:/C:/herong/Client.class All locations: file:/C:/herong/Client.class
Delete "Account.class", then load the "Client" class. I see no problem, because Account.class is not really needed to initialize the "Client" class.
herong> del Account.class herong> java -classpath . ClassCheck Client Class: Client Loader: jdk.internal.loader.ClassLoaders$AppClassLoader@1ff8b8f Loaded from: file:/C:/herong/Client.class All locations: file:/C:/herong/Client.class
Delete "Address.class", then load the "Client" class. The class loader fails during "Client" class initialization:
herong> del Address.class herong> java -classpath . ClassCheck Client Exception in thread "main" java.lang.NoClassDefFoundError: Address at Client.<clinit>(Client.java:3) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Unknown Source) at ClassChecker.main(ClassChecker.java:15) Caused by: java.lang.ClassNotFoundException: Address at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(...) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader .loadClass(...) at java.base/java.lang.ClassLoader.loadClass(...)
This proves that class initialization happens immediately after class loading.
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
►ClassLoader Class - Class Loaders
What Is java.lang.ClassLoader Class
Accessing the ClassLoader of a Class
loadClass() Method - Loading Classes Explicitly
getSystemResource() Method - Finding Files
Class Loading Problem - JAR Hell
ClassChecker.java - Reports Class Loader
ClassChecker.java - Reports Class Locations
"superclass access check failed" Class Load Error
►Class Loading Followed by Class Initialization
Class Class - Class Reflections
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations