Java static code blocks, construction code blocks, execution order of construction methods

Source: Internet
Author: User

Java static code blocks, construction code blocks, execution order of construction methods

static code takes precedence over non-static code because members that are modified by static are members of a class and are executed as the JVM loads the class, while members that are not statically decorated are also called instance members, and need to create objects to be loaded into heap memory. So static will take precedence over non-static.
When executing a constructor (constructor), there is an implicit three-step procedure before executing the method body:
1,super Statement , the following three scenarios may occur:
1) The first line of the constructor method body is the this statement, no implicit three steps are performed,
2) The first line of the constructor method body is the super statement, the constructor method of the corresponding parent class is called,
3) The first line of the constructor method body is neither the this statement nor the super statement, it is implicitly called super (), which is the default constructor of its parent class, which is why a parent class typically provides a default constructor method;
2, initialize non-static variables ;
3. Construct the code block .
It can be seen that constructing a code block takes precedence over the method body of the constructor method , but the This keyword cannot appear at the same time as the Super keyword, and only in the first line of code. If the This keyword appears, the implicit three-step is not executed.
For example, the following code and execution results are analyzed, and the step 1–step 7 has been marked with a comment.
That is, when multiple constructor methods are called recursively, constructing a block of code will only be executed before the final (also the first line of the method body is not the this statement) .

 Public  class Test {     Public Static intA =0;Static{//Step 1A =Ten; System.out.println ("Static code block is executing a="+ a); }    {//Step 4A =8; System.out.println ("Non-static code block (construction code block) is executing a="+ a); } Public Test() { This("Call with parameter construction method 1,a="+ a);//Step 2System.out.println ("No parameter construction method in execution a="+ a);//Step 7} Public Test(String N) { This(N,"Call with parameter construction method 2,a="+ a);//Step 3System.out.println ("with parameter construction Method 1 in execution a="+ a);//Step 6} Public Test(string s1, string s2) {System.out.println (S1 +";"+ s2);//Step 5} Public Static void Main(string[] args) {Test T =NULL;//JVM Load test class, static code block executionSystem.out.println ("A new test instance below:"); t =NewTest (); }}

Execution Result:

静态代码块在执行a=10下面new一个Test实例:非静态代码块(构造代码块)在执行a=8调用带参构造方法1,a=10;调用带参构造方法2,a=10带参构造方法1在执行a=8无参构造方法在执行a=8

http://bbs.csdn.net/topics/391001417

Java static code blocks, construction code blocks, execution order of construction methods

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.