Initialize BLOCK:
Includes static initialization blocks and normal initialization blocks.
Initialization blocks are added to the constructor at the beginning, and initialization blocks are always executed before the constructor.
The modifier for the initialization block can only be static.
An initialization block can be used by the system to initialize an object.
Basic usage:
If there is an initialization processing code that is exactly the same for all objects, and you do not need to receive any parameters, you can extract the initialization processing code into the initialization.
Advantages:
Improve the reuse of initialization code, improve the maintenance of the application.
The following code mainly describes static initialization blocks, ordinary initialization blocks, and the order in which the constructors are loaded:
/** * * @authorWuyong * @email [email protected] * @date2016 October 29 pm 4:13:45*///Three classes of inheritance relationships Public classA {Static{System.out.println ("A static initialization block"); } {System.out.println ("A's ordinary initialization block"); } PublicA () {System.out.println ("A's parameterless constructor"); }} Public classBextendsa{Static{System.out.println ("Static initialization block of B"); } {System.out.println ("B's ordinary initialization block"); } PublicB () {System.out.println ("B's parameterless constructor"); } PublicB (String string) {//calling the overloaded constructors in this class through this This(); System.out.println ("B has a parametric constructor, and his argument constructs the value string=" +string); }} Public classCextendsb{Static{System.out.println ("Static initialization block of C"); } {System.out.println ("Normal initialization block for C"); } PublicC () {Super("parameter passed into the B-argument constructor"); System.out.println ("C's parameterless constructor"); } /** Initialization blocks, constructors in the execution order of the inheritance relationship: * Not only do the initialization blocks, constructors in this class, * also go back to the object class, first execute the initial * block of the object, the constructor, and then execute its parent class ... * Finally, the initialization block, constructor is executed. * * * The first time a C object is created, because Class C does not exist in the system, so the class C must first be loaded and initialized, * The class C initializes the static initialization block of the parent class first, executes the static initialization block of its immediate parent class, and finally executes the static initialization block of C itself. * After successful class C initialization, Class C will always be present in the virtual machine, so do not initialize the Class C object for the second time, and do not perform static initialization blocks. * */ Public Static voidMain (string[] args) {System.out.println ("The first time you create a C object----"); NewC (); System.out.println ("The second time you create an object of C----"); NewC (); } /** Output: * A static initialization block B static initialization block C static initialization blocks the first time you create a C object----a normal initialization block a of the parameterless constructor B of the ordinary initialization block B of the parameterless constructor B of the parameter constructor, his argument constructs the value string= the parameter C passed into the B argument constructor of the ordinary initialization block C to execute the second time the non-parametric constructor c to create an object---- A general initialization block A of the generic initialization block B of the common initializer B of the parameterless constructor B of the argument constructor, his parameter construction value string= passed in the B parameter constructor of the parameters C of the ordinary initialization block Non-parametric constructor for C execution*/}
Object-oriented---static initialization blocks, normal initialization blocks, constructor load order