When does the initialization of the Java class take place? (Attached code)

Source: Internet
Author: User
Tags reflection
The class or interface is initialized when a Java program uses a class or interface for the first time in the following 6 ways.

(1) Create an instance of the class. The way you create instances for a class includes creating an instance with the new operator, creating an instance from reflection, and creating an instance by deserializing it.

(2) Call a class method (static method) of a class.

(3) access to a class variable or to a class variable. or assign a value to the variable

(4) Use reflection to force the creation of a Java.lang.Class object corresponding to a class or interface. For example: Class,forname ("person"), if the system has not initialized the person class, first initializes the person class, and then returns the Java.lang.Class object corresponding to the person class.

(5) Initializes a subclass of a class. The parent class of the subclass will be initialized.

(6) Run a main class directly using the Java.exe command, and the main class is initialized first.

Attention:

For a final type of class variable, if the value of the variable can be determined at compile time, then this class variable is equivalent to "macro variable", and the Java compiler will replace all occurrences of the class variable with its value directly at compile time. Therefore, even if the program uses static variables, it does not cause initialization of the class. The following code will not print out the static initialization block

    1. public class Test12 {static final String string= "macro variable"; static{system.out.println ("Static initialization Block");}} Class Test13{public static void Main (string[] args) {System.out.println (test12.string);}}

      Conversely, if the final decorated class variable cannot be determined at compile time, you must wait until the runtime to determine the value of the class variable, and if it accesses its class variable, it will cause the class to be initialized. (The following code will print out the static initialization fast)

public class Test12 {static final String string= "" +system.currenttimemillis () static{system.out.println ("Static initialization Block");}} Class Test13{public static void Main (string[] args) {System.out.println (test12.string);}
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.