Java class loading mechanism

Source: Internet
Author: User

The life cycle of the Java class loading mechanism class

The life cycle of a class consists of 7 phases: load, validate, prepare, parse, initialize, use, and unload

Initialization scenarios for classes
    1. When a specified bytecode instruction is encountered, 4 specified bytecode, new, putstatic, getstatic, and Invokestatic
    2. Initialization of a parent class is triggered during subclass initialization if the parent class is not initialized
    3. Initialization is triggered when a reflection call is made to a class using a method in a reflection package.
    4. When the JVM starts, the user specifies a main class to be executed, and the virtual opportunity triggers the initialization of that main class
    5. When dynamic language support is in JDK1.7, it is also initialized if the method handle has a static keyword.
Class non-initialized scene scenario one:

When you refer to a static variable of a parent class through a subclass, the initialization of the subclass does not occur

The code is as follows:

PublicClassssclass{static {System.out.println ("Ssclass"); }}PublicClassSuperclassExtendsssclass{static {System.out.println ("Superclass init!"); }PublicStaticint value =123;PublicSuperclass() {System.out.println ("Init superclass"); }}PublicClasssubclass extends SuperClass{static {System.out.println ( "subclass Init");} static int A; public SubClass  () {System.out.println ( "Init subclass");}} public class notinitialization{public static void main       

Results of the output:
Ssclass
Superclass init!
123

Scenario Two:

The class is referenced by an array and does not trigger the class initialization such as: subclass[] array = new SUBCLASS[10] Because this time the bytecode instruction is NewArray

Scenario Three:

When a class variable is decorated with the final keyword, referencing constanvalue does not trigger the class initialization
Cause: This constant has been stored in the constant pool during the preparation phase of the class, and the call is not much related to the class

Apply test questions

Package com.zzjmay;PublicClassTESTJVM {PublicStaticvoidMain(string[] args) {staticfunction ();}static TESTJVM st = new TESTJVM (); static {System.out.println ( "1");} {System.out.println ( "2");} TESTJVM () {System.out.println ( "3"); System.out.println ( "a=" +a+ "b=" +b);} public static void staticfunction () {System.out.println ( "4"); } int a=110; static int b =112;}     

The result of the output is
2
3
a=110, b=0
1
4

Attention:
The procedure for initializing an object is to initialize the member variable before initializing the constructor
class initialization is performed in code order, and object initialization is performed first if there is nested object initialization.

Welcome to join the Learning Exchange Group 569772982, we learn to communicate together.

Java class loading mechanism

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.