Java code block

Source: Internet
Author: User

1. Common code blocks:

Local code blocks, which appear in methods, limit the life cycle of variables, release early, and improve memory utilization.

2. Construct the code block :

  Occurs outside of a method in a class, where the same code is stored together in multiple construction method methods, each call construct executes, and is executed before the method is constructed.

3. Static code block:

  Occurs outside of a method in a class, with a static modifier, to initialize the class, execute at load time, and execute only once.

4. Synchronizing code blocks:

Using synchronized () {} Wrapped blocks of code, in a multithreaded environment, the read and write operation of the shared data is mutually exclusive, otherwise it will result in inconsistencies in the data. The synchronization code block needs to be written in the method.

Example 1:

class Student {    static  {        System. out. println ("Student Static code block ");    }    {        System. out. println ("Student Construction code block ");      Public Student () {        System. out. println ("Student construction method ");}    }    

class demo2_student {    static  {        System.out.println ("demo2_student code block");    }     publicstaticvoid  main (string[] args) {        System.out.println ("I am Main method ");         New Student ();         New Student ();    }}

Execution process: First, load class Demo2_student, execute static code block when loading. The main method is then executed. Creating a Student object requires loading the class first, and then executing a static block of code when the class is loaded. Then, before the new object executes the construction code block, the paparazzi method is executed.

Example 2:

class test2_extends {    publicstaticvoid  main (string[] args) {      New  Zi ();    }}
class Fu {    static  {        System.out.println ("Static code block Fu");    }    {        System.out.println ("construct code block Fu");    }      Public Fu () {        System.out.println ("Construction method Fu");}    }
class extends Fu {    static  {        System.out.println ("Static code block Zi");    }    {        System.out.println ("construct code block Zi");    }      Public Zi () {       System.out.println ("Construction method Zi");}    }

Execution process:

1,JVM called the Main method, the main input stack
2, encountering Zi z = new Zi (), the Fu.class and Zi.class are loaded into memory first, and then the object is created, when Fu.class loads the static code block of the memory parent class along with the Fu.class, when zi.class loads into memory, the subclass's static Code blocks are loaded along with Zi.class. First output static code block Fu, second output static block of code Zi
3, go to the Zi class constructor method, because Java is hierarchical initialization, first initializes the parent class, and then initializes the subclass, so the first walk of the parent class constructs, but in the execution of the parent class constructs, discovers the parent class has constructs the code block, constructs the code block is takes precedence over constructs the method executes the code block Fu, Fourth output construction Method Fu
4,fu class initialization, subclass initialization, fifth output is the construction code block Zi, construction method Zi

Java code block

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.