Java class running and execution sequence of static Blocks

Source: Internet
Author: User

I. When will the static block of the Java class be executed?
If you understand the JVM principles, we can know that the operation of a class is divided into the following steps:
1. Load; 2. Link; 3. Initialization; 4. Initialization; 5. instantiation (create a new instance)
In fact, the execution of the static block occurs in the "initialization" phase. In the initialization phase, JVM initializes static variables and executes static blocks.
Let's take a look at several cases of executing static blocks:
1. The first new A () process will print ""; because this process includes Initialization
2. The first class. the forname ("A") process prints ""; because this process is equivalent to class. forname ("A", true, this. getclass (). getclassloader ());
3. The first class. forname ("A", false, this. getclass (). getclassloader () process will not print "". Because false indicates that initialization is not performed during the course of loading the class. Static blocks are not executed without initialization.
Ii. execution sequence of static in Java class
If the method is defined as static, it means that this method can be executed without declaring an instance of the class; for example, the main method. When a class member is defined as static, it is almost completely independent of this class.
According to think in Java2 and Java, the static method is executed when the class is loaded.
Therefore, it can be said that the static method is executed when the program is interpreted.
For example
Class usestatic {
Static int A = 3;
Static int B;

Static void meth (int x)
{
System. Out. println ("x =" + x );
System. Out. println ("A =" + );
System. Out. println ("B =" + B );

}
Static {
System. Out. println ("B =" + B );
}
Static {
System. Out. println ("static block initialized .");
B = A * 4;
}
Public static void main (string [] ARGs)
{
Meth (42 );
}
}
Once usestatic is loaded, all static statements are executed. First, a is set to 3, and then system. out. println ("B =" + B); print out B = 0. Note that multiple static code blocks can be executed in sequence, and B is initialized to 0 by default, then execute the static block (print a message). Finally, initialize B to a * 4 or 12. Then, call main () and main () to call meth () and pass 42 to X. The three println () Statements reference two static variables A and B, and the local variable X.

Iii. Research on execution sequence of extends and static

Public Class {
Public static final String constant = "1 ";
Static {
System. Out. println ("");
}
Public (){
System. Out. println ("B ");
}
}

Public Class B extends {
Static {
System. Out. println ("C ");
}
Public B (){
System. Out. println ("D ");
}
}

Public class test {

Public static void main (string [] ARGs ){
System. Out. println (A. Constant );
A AB = new B ();
AB = new B ();
}
}
But note that the code below will not execute the static Block
System. Out. println (A. Constant );
Because this is a static constant of the class, it will not cause the class to be loaded when calling the static constant of the class (this is inferred from the result, there is no relevant explanation for the check on the Internet. Maybe it is the JVM rule)
1
A
C
B
D
B
D
It is printed when AB = new B ();
B
D
This indicates that the parent class will also be instantiated during subclass instantiation.

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.