Java double parenthesis Initialization

Source: Internet
Author: User

Java double parenthesis Initialization
Common initialization Methods

Java can be initialized using parentheses and can be divided into static code blocks and non-static code blocks. As follows:

public class Test {    public static int num = 0;    String s = ;     {        s = abc;        System.out.println(non static init);    }    static {        num = 1;        System.out.println(static init);    }    public void runTest(){        System.out.println(num);        System.out.println(s);    }}

Run the runTest method of the Test class and the output is as follows:

Static init
Non static init
1
Abc

The output results show that static code blocks are executed before non-static code blocks.

In addition, non-static code blocks differ from static code blocks:
Static code blocks are loaded and executed only once when the VM loads classes;
A non-static code block is executed when an object is created (that is, when a new object is created). Each time an object is created, it is executed once.

To verify the preceding differences, write the test program as follows:

Test test1 = new Test();Test1.runTest();Test test2 = new Test();test2.runTest();

The output result is as follows:

Static init
Non static init
1
Abc
Non static init
1
Abc

Verify that the static code block is only executed once, and that the non-static code block is executed every time it is created.

Initialization form

In addition to the above code block initialization method, Java also has an initialization method, that is, double parenthesis initialization. The format is as follows:

    Test test = new Test(){        {            num = 2;            s = aaa;        }      };    test.runTest();

The output result is as follows:

Static init
Non static init
2
Aaa

This initialization method is actually usedAnonymous class. The outer square brackets actually create an anonymous internal class, while the inner square brackets are the constructor of the anonymous class, so you can directly Insert the initialization code.

If we print a piece of information in the internal constructor and the constructor in parentheses, we can get the following results:

Static init
Non static init
Inner constructor
Brackets constructor
2
Aaa

The code in the brackets is later than that in the internal constructor.

What are the advantages and disadvantages?

Although the double-bracket initialization method simplifies programming readability, to create an anonymous internal class, the anonymous internal class will hold the reference of the external class, which may cause memory leakage.

 

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.