Static member variables for parsing Java classes are initialized before non-static member variables

Source: Internet
Author: User

The class bytecode file is loaded into memory by the JVM virtual machine, followed by

    • Connection
      • Verification: Byte code verification
      • Prepare: Allocates memory for static variables and assigns default values for variable types (note: The base type is 0 or false, and the object is directly assigned the corresponding value for the null,static final modifier)
      • Parse: A symbol reference in a class is converted to a direct reference
    • Initialize: initializes the corresponding value for the static variable/static statement block of the class

and the class initialization opportunity is: class in the case of the first active use , the active use includes the following 6 cases:

    1. When instantiating an object with the New keyword.
    2. When a static method of a class is called.
    3. When reading or setting a static field for a class (except when it is final decorated, the result is placed in a constant pool at compile time).
    4. Use the Java.lang.reflect package method to make reflection calls to the class.
    5. When a subclass of a class is initialized.
    6. When the virtual machine starts, it is marked as the startup class (the class that contains the main method).

From above we know that the initialization of a static member variable occurs during the initialization phase of the class, and to instantiate an object you must first load, concatenate, and initialize the class before you can use it to instantiate the object, not the

The initialization of static member variables occurs during the instantiation of an object. The following example shows that when a class is first actively using static blocks or static member variables, it is initialized.

1 package com.space.equalstest; 2  3 public class Dog {4 public     static String name = "Tom"; 5  6     static {7         System.out.println ("Dog INI T "); 8     } 9 public     static void info () {         System.out.println ("a Dog");     }13}
1 Package com.space.equalstest;2 3 public class Test {4 public     static void Main (string[] args) {5         dog.info ();//First The active use is initialized with 6         dog.info ();//No more initialization of 7     }8}

Output Result:

123 Dog inita doga dog

  

Static member variables for parsing Java classes are initialized before non-static member variables

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.