Code block, java code block

Source: Internet
Author: User

Code block, java code block

Code block classification:

1. Common Code Block

2. Construct a code block

3. Static code block

4. synchronous code block

The code block cannot run independently and must depend on other configurations. The format is: {// code block }.

1. Common Code Block

A common code block that is enclosed by a pair of "{}" after the method name (or in the method body) and called by a method.

Package cn.com. daimakuai;/*** PuTongDaiMaiKuai * @ author: hushaoyu * 9:27:02 * @ Description: common code block */public class PuTongDaiMaiKuai {public static void main (String [] args) the common code block enclosed by {/** "{}". The variable x = 100 ranges from "{" start, therefore, the following variables x = 200 do not affect each other. * if you put the following variables in front of the common code, the compilation is incorrect because the main code block range is large */{int x = 100; system. out. println ("common code block:" + x);} int x = 200; System. out. println ("main method:" + x);}/*** result output: common code block: 100main method: 200 */}
2. Construct a code block to construct a code block: A code block defined directly in the class without any prefix, suffix, or modifier.

The constructor code block is called when the object is generated, but the call time is earlier than the constructor method. Therefore, the constructor code block can be used to initialize member variables. If a class has multiple constructor methods and all these constructor variables need to be initialized, you can extract the same code from each constructor and put them together in the constructor code block, construct code blocks to initialize common member variables and reduce repeated code.

Package cn.com. daimakuai;/*** ConstroctDaimakuai * @ author: hushaoyu * @ 10:02:55 * @ Description: create a code block */public class ConstroctDaimakuai {public static void main (String [] args) {Person p1 = new Person (); System. out. println ("********************"); Person p2 = new Person ("hushaoyu ");}} // Person class Person {private int x; private String name; // construct the code block {System. out. println ("construct a code block to execute -------"); // performs the initial operation on the member variable x of the class. To achieve the same effect, if it is not placed in the code block, it must appear in two constructor methods respectively. X = 100;} // No parameter constructor Person () {System. out. println ("constructor -------"); name = "huyuyu"; show () ;}// the constructor Person (String name) {System. out. println ("constructor -------"); this. name = name; show ();}/*** show Method */private void show () {System. out. println ("welcome! "+ Name); System. out. println (" x = "+ x);}/** result output: Construct a code block and execute ------- construct a method to execute ------- welcome! Huyuyux = 100 ********************* construct a code block and execute ------- construct a method to execute ------- welcome! Hushaoyux = 100 */}
3. Static code block

Static code block: the static code block that is modified and enclosed.

Feature: it is used to initialize static member variables. It is first executed (loaded with the class) and only once.

Package cn.com. daimakuai;/*** Staticdaimakuai * @ author: hushaoyu * 10:45:49 * @ Description: static code block */public class Staticdaimakuai {public static void main (String [] args) {System. out. println ("main method execution -------"); System. out. println ("*** create 1st objects ***"); new Staticdaimakuai (); System. out. println ("*** create 2nd objects ***"); new Staticdaimakuai (); System. out. println ("*** create 3rd objects ***"); new Staticdaimakuai ();} // static code block static {System. out. println ("static code block execution -------");} // constructor Staticdaimakuai () {System. out. println ("constructor ------");} // construct the code block {System. out. println ("construct code block execution ------");}/** result output: static code block execution ------- main method execution ------- *** create 1st objects *** construct code block execution ------ construction method execution ------ *** create 2nd objects *** construct code block execution ------ constructor execution ------ *** create 3rd objects *** construct code block execution ------ constructor execution ------*/}
4. synchronous code block Synchronous Code block: Used in multithreading.

 

  

 

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.