About static code block, common code block, synchronous code block, constructor and constructor

Source: Internet
Author: User

About static code block, common code block, synchronous code block, constructor and constructor


Constructor
Used to initialize an object. It is a type of function that initializes the corresponding object.
Features:
1: The name of the function is the same as that of the class.
2: the return value type does not need to be defined.
3: This function does not return a specific value.
Remember: all objects must be initialized before they can be used.

NOTE: If no constructor is defined for a class, an empty constructor is automatically generated for the class. To facilitate object creation and initialization of the class. If you customize the constructor in the class, the default constructor will no longer exist.

A class can have multiple constructors. Because their function names are the same, they can only be distinguished by parameter lists. Therefore, if multiple constructors exist in a class. They exist in heavy loads.

What is the difference between a constructor and a general function?
1: The two function definition formats are different.
2: The constructor is called when an object is created and used for initialization. The initialization action is executed only once.
A common function is executed only after an object is created. It can be called multiple times.

When can I use constructors?
When analyzing things, we find that specific things have some features as soon as they appear, So we define these features into the constructor.

What is the difference between a constructor and a constructor?
Construct a code block: initializes all objects. That is, all objects call a code block. As long as the object is created. This code block is called.
Constructor: initializes the object corresponding to it. It is targeted.

Common Code Block

The {} That appears directly in a method is called a common code block. The example program is as follows:
Public class CodeDemo01 {public static void main (String [] args) {// common code block {int x = 10; System. out. println ("x =" + x);} int x = 100; System. out. println ("x =" + x );}

Construct code blocks

The code block {} defined directly in the class without the static keyword is called the Construction Code block. The example program is as follows:

Public class CodeDemo02 {public CodeDemo02 () {System. out. println ("========= this is the constructor ==========");} // This is the Construction Code block, and in the new object, construct a code block. Execute {System. out. println ("========= this is the construction block! ======== ");} Public static void main (String [] args) {new CodeDemo02 (); new CodeDemo02 ();}}
Static code block
It is a block area with static keywords. Defined in the class.

A code block declared using the static keyword is called a static code block. The main purpose of a static block is to initialize a static property. The example program is as follows:
 
Public class CodeDemo03 {static {System. out. println ("this is a static code block in the main class! ");} Public static void main (String [] args) {new Demo (); new Demo () ;}} class Demo {static {System. out. println ("this is a static code block in the Demo class! ") ;}{ System. out. println (" this is the construction block in the Demo class! ");} Public Demo () {System. out. println (" this is the constructor! ");}}

The static block takes precedence over the execution of the main method, and the static block takes precedence over the execution of the constructor method!

Function: class initialization can be completed.
The static code block is executed as the class is loaded, and only once (multiple new objects are executed only once ). In the same class as the primary function, the execution of the primary function takes precedence over that of the primary function.
Static blocks are executed when a class is loaded. let's first talk about class loading. To run a program, load the code to the memory first, right? Then you can communicate with the CPU,
This is what von noriman computer requires. The same is true for Java. to execute a class bytecode file, you must first load it To the memory. The class loader loads the code of the bytecode file into the memory. This step is called class loading, this is the first step.
Public class Test {static {System. out. println ("I Am a static block ");}}
When creating an object of the Test class, such as new Test (), yes. First, the class is loaded before the new object is created. The static block is executed when the class is loaded,
This indicates that the static block will be executed before the new object, and a class will be loaded when it is used for the first time, then, the application will not be loaded again during the entire lifecycle, so this indicates that the static block will be executed once and will not be executed for the second time!
Public class Test {public Test () {// constructor System. out. println ("I Am a constructor. I will execute it when I create an object. After I finish the execution, the object will be created.");} static {System. out. println ("I Am a static block, I executed it when the class was loaded, and only executed this time ");}}
Then:
New Test ();
New Test ();
You will find that the information of the static block is printed first, then the information of the constructor is printed, and then new Test (); again, only the information of the constructor is printed, this example proves that what I mentioned above is correct!

This is the static block, and the Map is initialized in the static block.
Next we can say that the Map is initialized in the static block,
public class Test {private static Map m;static {m = new HashMap();}}

Static code blocks are automatically executed when a class is loaded. They are executed only once before the main method. They are often used to initialize attributes of some classes.
The construction code block is executed every time an object is created.

Synchronous Code block.
If the synchronized keyword is added before a code block, the code block becomes a synchronous code block. Synchronous Code block format: synchronized (synchronization object) {code to be synchronized;} the synchronization object is generally the current object, that is, use the this keyword
Package cn. sunzn. synchronize; public class SynchronizeCode {public static void main (String [] args) {new Thread () {public void run () {while (true) {System. out. println ("synchronization code ");}};}. start (); new Thread () {public void run () {while (true) {System. out. println ("SynchronizeCode ");}};}. start ();}}

Result:

SynchronizeCodeSynchronizeCodeSynchronizeCodeSynchronizeCode Synchronization Code
Execution sequence of static code blocks, constructor blocks, and constructor
Static code block --> construct a code block --> constructor;
That is, execute the static code block first, and then construct the code block. The next step is the constructor. If it is in the main class. The execution sequence is: Execute the static code block first. Construct the code block While executing the main method. The next step is the constructor.
 

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.