Timing of JVM load classes in Java

Source: Internet
Author: User

First, describe

When are classes in Java loaded by a JVM virtual machine? Because static member initialization is performed when the class is first loaded, we test the execution time of a static block of code to determine when the JVM loads the class.

Summary: TheJVM is called only if it needs a class or uses Class.forName (className) to force the class to be invoked, and if it simply declares a reference to a class and does not create the object, the class is not loaded.


Second, the source code

1. Simply declaring a reference to a class without creating an object the JVM will not load the class

Package tong.yue.day4_25;/** * When is the class in Java loaded by the JVM virtual machine? Because static member initialization is performed when the class is first loaded, we determine when the JVM loads the class by testing the execution time of a static block of code. * Summary: The JVM is called only when a class is required or forced to load the class using Class.forName (ClassName). *  * @author Tong *  */class loadclass {static {System.out.println ("JVM load Class ...");} public static int height = 30;public static void print () {System.out.println ("Execute print () function ...");}} public class Loadclasstest {public static void main (string[] args) {//Declare only a reference to a class without creating an object the JVM does not load the class Loadclasstest LoadClass;}}

The above code does not output anything at the time of execution, nor does it execute a static block of code, so confirm only a reference to a class is declared, and no object is created, and the JVM does not load the class.

2. Whenever you create an object of a class, or use a static member variable of a class, or use a static method of a class, the JVM will load the class

Package tong.yue.day4_25;/** * When is the class in Java loaded by the JVM virtual machine? Because static member initialization is performed when the class is first loaded, we determine when the JVM loads the class by testing the execution time of a static block of code. * Summary: The JVM is called only when a class is required or forced to load the class using Class.forName (ClassName). *  * @author Tong *  */class loadclass {static {System.out.println ("JVM load Class ...");} public static int height = 30;public static void print () {System.out.println ("Execute print () function ...");}} public class Loadclasstest {public static void main (string[] args) {//only if there are any of the following three sentences, the class is loaded by the JVM and output once: The JVM loads the class ... LoadClass loadclass = new LoadClass ();  1system.out.println (loadclass.height);   2loadclass.print ();                      3}}

Operation Result:

3. Use the Class.forName (ClassName) method to force the JVM to load the class, and if the class has not yet been loaded by the JVM, the JVM will load the class and will not load if the class has already been loaded.

Package tong.yue.day4_25;/** * When is the class in Java loaded by the JVM virtual machine? Because static member initialization is performed when the class is first loaded, we determine when the JVM loads the class by testing the execution time of a static block of code. * Summary: The JVM is called only when a class is required or forced to load the class using Class.forName (ClassName). *  * @author Tong *  */class loadclass{static {System.out.println ("JVM load Class ..."); public static int height = 30;public static void print () {System.out.println ("Execute print () function ...");}} public class Loadclasstest {public static void main (string[] args) {//Use the Class.forName (ClassName) method to force the JVM to load the class, The parameter inside is the full path to the class: Package name + class name try {class.forname ("Tong.yue.day4_25.LoadClass");} catch (ClassNotFoundException e) { E.printstacktrace ();}}}
Operation Result:



Iii. Summary

1. The JVM load class is a lazy-loading pattern that is loaded only when a class is needed, but is not loaded when declaring a reference to a class without creating an object;

2. The JVM loads the class object when it is created in the program, or if it does not create the class object but calls the static method of the class, or uses the static members of the class.

3. When the program uses Class.forName (className) to force the JVM to load a class, if the class is not already loaded by the JVM, the JVM will load the class and will not load if the class is loaded.

Timing of JVM load classes in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.