Analysis of construction code block, static code block and construction method in Java

Source: Internet
Author: User

What is the difference between building blocks of code, static blocks of code, and constructing methods that are three different blocks of code?

First, a simple understanding of the construction code block, static code block and construction method

Class a{//constructs the code block {System.out.println ("Construct code block A"); Static code block STATIC{SYSTEM.OUT.PRINTLN ("Static code block A");} Construction method Public A () {System.out.println ("constructor method A");}}

Ii. to ascertain the order of execution of three persons

Class Demo {public static void main (string[] args) {new A ();}}

Thirdly, what is the order of constructing multiple objects?

Class Demo {public static void main (string[] args) {new A (); new A (); new A ();}}

Iv. See how it will be in the inheritance system

Class B extends a{//constructs the code block {System.out.println ("Construct code block B"); Static code block STATIC{SYSTEM.OUT.PRINTLN ("Static code block B");} Construction method Public B () {System.out.println ("construction method B");}}

Class Demo {public static void main (string[] args) {new B ();}}

Five, summarize1, in the process of creating an object, the execution order of the three is: Static code block--Construction code block---construction method;2, each object is created, the construction code block and construction method will be executed once, regardless of how many objects are created, static code block is executed only once;3, when the subclass object is created, the static code block of the subclass executes 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 you create a subclass object, the child class constructs a block of code that executes after the parent class's construction method.
Six, talk about the otherstatic code blocks are similar to static variables and static methods, and all objects share one copy, which is executed the first time the class is loaded. Static code blocks are primarily used for the initialization of classes, while the other is used for initialization of objects. Say a half-day static code block, then what exactly does it do? Time is urgent, give examples to be considered, but suffice to explain the problem. an application needs to access the database and must specify a connection string in order to obtain a connection. For a program, the connection string is generally fixed after it is run. A, set in the construction code block or construction method, then each time you create the object must be set once, repeat, very troublesome. B, in the program to write the connection string dead, that is, a constant to save the connection string. So, in case the next time you run a connection string, you have to go to the code to take the changes, trouble ...for this example, if we set the connection string in a static code block, we can solve the problem in A and B. First, the static block of code executes only once when the class is loaded, avoids duplication, and secondly, if the connection string is changed, the connection string in the code is dynamically changed because it is loaded once each time it is run.



Analysis of construction code block, static code block and construction method in Java

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.