Initializes the class's life cycle (lower) class to "Go"

Source: Internet
Author: User

Up-depth Java Virtual machine-- deep Java Virtual Machine (ii)--Class loader details (above), in the previous article, we explained the life cycle of the class loading and linking, this one we went on to look down.

class Initialization : Initialization of the class begins after the class's life cycle has finished loading and joining. During the initialization phase of the class, the Java virtual machine executes the initialization statement of the class, assigning a value to the static variables of the class, in which the class is initialized in two ways: (1) Assigning a value at the declaration of the variable. (2) Assign a value at a static code block, such as the following code, a is the first initialization, B is the second initialization

[HTML]View Plaincopyprint?
    1. public class Test
    2. {
    3. public static int a = 0;
    4. public static int B;
    5. static{
    6. b=2;
    7. }
    8. }



Both the declaration of static variables and the initialization of static code blocks can be considered as initialization of static variables, and the initialization of static variables of classes is sequential. Sequence for class files from top to bottom to initialize, think of this, think of a very shameless face test , share to everyone to see:

[Java]View Plaincopyprint?
  1. Package com.bzu.csh;
  2. Class Singleton
  3. {
  4. Private static Singleton Singleton = new Singleton ();
  5. Public static int counter1;
  6. Public static int counter2 = 0;
  7. Private Singleton ()
  8. {
  9. counter1++;
  10. counter2++;
  11. }
  12. Public static Singleton getinstance ()
  13. {
  14. return singleton;
  15. }
  16. }
  17. Public class Test
  18. {
  19. Public static void Main (string[] args)
  20. {
  21. Singleton Singleton = Singleton.getinstance ();
  22. System.out.println ("counter1 =" + Singleton.counter1);
  23. System.out.println ("counter2 =" + Singleton.counter2);
  24. }
  25. }



Let's see what the program will output here.

Don't know what the answer is, if you don't mind, you can write your answer to the comment and see how many people have the same answer as you. Let me first talk about the answer I just started. I think it will output:

Counter1 = 1

Counter2 = 1

I don't know if everyone's answer is this, anyway, mine is. Let's take a look at the correct answer:

I don't know if you did it right, but I just started doing it wrong. OK, now let me explain why this is the answer. Before giving an explanation, let's start by looking at a concept:

The way Java programs use classes can be divided into two types

Active use

Passive use

• All Java Virtual machine implementations must initialize each class or interface when it is " first active " by a Java program

Active use (six types)

– Create an instance of the class

– Access a static variable for a class or interface, or assign a value to the static variable

– Call the static method of the class

– Reflections (e.g. Class.forName ("Com.bzu.csh.Test"))

– Initializes a subclass of a class

–java class (Java Test) that is identified as the startup class when the virtual machine is started

OK, we start to explain the above answer, the program begins to run, first executes the main method, executes the first statement of the main method, invokes the static method of the Singleton class, where the static method of calling the Singleton class is to actively use the Singleton class. So start loading the singleton class. In the process of loading the Singleton class, the static variable is assigned the default value first.

Singleton=null

Counter1 = 0

Counter2 = 0

After assigning them the value of the default value, the only thing to do is initialize the static variable and initialize the variable that was already assigned at the time of the Declaration. As we mentioned above, initialization is assigned from top to bottom of the class file. So first assign the value to the singleton, assign it a value, it is necessary to execute its construction method, and then execute counter1++;counter2++; so here's Counter1 = 1;counter2 = 1; After performing this initialization, Then perform the initialization of the counter2, and when we declare it, we initialize it to 0, so the value of Counter2 becomes 0. The output is executed when the initialization is complete. So that's it, yes

Counter1 = 1

Counter2 = 0

Class initialization steps

(1) If a class has not been loaded or connected, load and connect the class first

(2) If the class has a direct parent class, and the parent class is not initialized, initialize the immediate parent class first.

(3) If there is an initialization statement in the class, execute these initialization statements directly in sequence

On top of us, we said that the Java Virtual Machine implementation must initialize them when each class or interface is " first active " by the Java program, with six instructions for active use. In addition to the six scenarios described above, other ways of using Java classes are considered passive and do not cause initialization of classes. The "active use" of a subclass in a program causes the parent class to be initialized, but the "active" use of the parent class does not cause the subclass to initialize (it is impossible to say that an object of the class is generated and all subclasses in the system are initialized)

Note: calling the LoadClass method of the ClassLoader class loads a class, not the active use of the class, and does not cause the initialization of the class.

When a Java Virtual machine Initializes a class, all of its parent classes are required to be initialized, but this rule does not apply to the interface.

When a class is initialized, the interface it implements is not initialized first

When an interface is initialized, its parent interface is not initialized first

Therefore, a parent interface is not initialized because of its sub-interfaces or the initialization of the implementation class. The initialization of the interface is only caused when the program first uses a static variable for a particular interface. Active use of a class or interface can be considered only if the static variable or static method accessed by the program is indeed defined in the current class or in the current interface. The subclass is not initialized if it is the parent class property of the called Child class.

Initialization of the

class's life cycle (bottom) class "Go"

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.