The construction code block, static code block and Construction Method in Java are analyzed.

Source: Internet
Author: User

The construction code block, static code block and Construction Method in Java are analyzed.

There are three different code blocks: constructing code blocks, static code blocks, and construction methods. What are their differences?

I. Briefly understand how to construct code blocks, static code blocks, and constructor Methods

Class A {// construct the code block {System. out. println ("construct code block A");} // static code block static {System. out. println ("static code block A");} // constructor public A () {System. out. println ("constructor ");}}

Ii. Clarify the execution sequence of the three elements

class Demo {public static void main(String[] args) {new A();}}

3. What is the order of constructing multiple objects?

class Demo {public static void main(String[] args) {new A();new A();new A();}}

4. Let's see what will happen in the inheritance system.

Class B extends A {// construct the code block {System. out. println ("construct code block B");} // static code block static {System. out. println ("static code block B");} // constructor public B () {System. out. println ("constructor B ");}}

class Demo {public static void main(String[] args) {new B();}}

5. Summary 1. In the process of creating an object, the execution sequence of the three is: static code block --> construction method; 2. Each creation of an object, the construction code block and construction method are executed once. No matter how many objects are created, the static code block is only executed once. 3. When a subclass object is created, the static code block of the subclass is executed after the static code block of the parent class, but takes precedence over the Construction Code block and construction method of the parent class. 4. When creating a subclass object, the constructor block of the subclass is executed after the constructor of the parent class.
6. Let's talk about other static code blocks, which are similar to static variables and static methods. All objects share one copy and are executed when the class is loaded for the first time. Static code blocks are mainly used for class initialization construction, while the other two are used for object initialization. After talking about the static code block for half a day, what is the use of it? The time is urgent. The example is to be considered, but it is sufficient to explain the problem. An application needs to access the database. To obtain a connection, you must specify a connection string. After a program is running, the connection string is usually fixed. A. Set it in the constructor code block or constructor, so you must set it once every time you create an object. It is repetitive and troublesome. B. Write the connection string to death in the program, that is, use a constant to save the connection string. So, if you change the connection string before the next operation, you must modify it in the code ...... In this example, if we set the connection string in the static code block, we can solve the problems in a and B. First, the static code block is executed only once when the class is loaded to avoid duplication. Second, if the connection string is changed, because it is loaded once before each operation, that is, the connection string in the Code is dynamically changed.

Zookeeper

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.