Java reading Note 12 (code block in Java)

Source: Internet
Author: User

Preface

We know that in Java the object is initialized with a constructor, but in Java, like the constructor, there are initialization block operations, the following small part to share for you.

Initialize block using

The initialization block in Java is the 4th member that appears in Java, with the first three being member variables, methods, and constructors. There can be more than one initialization block in a class, and initialization blocks of the same type are executed sequentially in order. The modifier for the initialization block can be static only, and the initialization block may contain any executable statements, including defining local variables, invoking other object methods, and using branches, looping statements, etc.

/**      * @FileName: Test.java    * @Package: Com.number    * @Description: TODO   * @author: LUCKY     * @date : November 29, 2015 PM 12:55:00    * @version V1.0      */package com.number;/** * @ClassName: Test * @Description: TODO * @author : LUCKY * @date: November 29, 2015 PM 12:55:00 */public class Test {int Cd;int d=9; {//The following defines an initialization block int a = 6; System.out.println (A > 4?) "Local variable A is greater than 4": "Local variable A is less than 4");} int c=0;    Public  Test () {    cd=10;    SYSTEM.OUT.PRINTLN ("initialization constructor");    } public static void Main (string[] args) {test test = new test (); System.out.println (Integer.compare (2, 3));}}

initializing blocks and constructors

To some extent, the initialization block is a supplement to the constructor, and the initialization block is always executed before the constructor, and initialization of the object can be done using the initializer.

But the initialization block differs from the constructor in that it is just a fixed code of execution and cannot accept any arguments. Therefore, the application scenario of the initializer can be understood as that when two or more constructors have the same initialization code, and these initialization codes do not have to accept parameters, they can be defined in the initialization block. By extracting the same code from multiple constructors into the initialization definition, it can improve the reuse of initialization code and improve the maintainability of the whole application.

Static initialization blocks

If the static modifier is used when defining the initialization block, the initialization block becomes a static initialization block, also known as a class initialization block (the normal initialization block is responsible for initializing the object, and the class initialization block is responsible for initializing the class). Static initialization blocks are class-dependent, and the system performs static initialization blocks during the class initialization block phase, rather than when the object is created. So static initialization blocks are always executed first than normal initialization blocks.

Needless to say, take a look at the following demo, to relieve boredom! Be sure to run through it carefully.

Package Com.test;class root{static{system.out.println ("Static initialization block of Root");} {System.out.println ("Root's normal initialization block");} Public Root () {System.out.println ("root parameter-free constructor");}} Class Mid extends Root{static{system.out.println ("Static initialization block for Mid");} {SYSTEM.OUT.PRINTLN ("Normal initialization block for mid");} Public Mid () {System.out.println ("parameter-free constructor for mid");} Public Mid (String msg) {//The constructor that is overloaded in the same class is called through this (); System.out.println ("Mid with parametric constructor, whose parameter value:" + msg);}} Class Leaf extends Mid{static{system.out.println ("static initialization block of the Leaf");} {SYSTEM.OUT.PRINTLN ("normal initialization block of the Leaf");} Public Leaf () {//The constructor super ("Crazy Java Handout") that has a string argument in the parent class via super; System.out.println ("The constructor that executes the leaf");}} public class Test{public static void Main (string[] args) {new Leaf ();}}


Java reading Note 12 (code block in Java)

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.