Object-oriented initialization block of Java

Object-oriented initialization block of Java

In programming, so that data fields correctly perform the initialization has been a timeless truth.
So, what it means to be initialized data field:

  • Setting values ​​in the constructor.
  • Assignment in a statement.
  • Use initialization block .

Discussion Benpian attention to points on Java initialization block: when in Java initialization block is a member of the class, but neither the name nor logo, can not be invoked, it merely implicit in the creation of Java objects perform initialization.

Common initialization block

  • Generic initialization block is non-static modification.
  • When the statement braces {}wrapped codes, initialization code is encased, initialization is a whole block.
  • There can be many initialization block, and all have performed in sequence, so no need to separate, get away together.
  • Specify default values when declaring instance variables and the normal initialization block can be seen as an object initialization code is executed according to the order.

  • Initialization block is always called before the constructor .
  • If a plurality of constructors have the same overloaded and the incoming independent parameter statement can be put into the initialization block together.

public class NormalBlock {
    int a = 5;
    {
//        a = 6;
        System.out.println("初始化块之后的a为"+a);
    }
//    {
//        int a = 8;
//        System.out.println("初始化块中重新定义了一个a?"+a);
//    }
    NormalBlock(){

        System.out.println("构造器中赋a的值为"+a);
    }
}
class NormalTest{
    public static void main(String[] args) {
        new NormalBlock();
    }
}
  • NOTE above statement, as follows:
初始化块之后的a为5
构造器中赋a的值为5

It can be seen in this example, an instance variable declarations specify default values ​​are also considered initialization code, and sequentially performed, the first initialization block, the constructor. (You can try to swap their position verification Kazakhstan)

  • After the lifting of the above comment statement, I was wondering the result of:
初始化块之后的a为6
初始化块中重新定义了一个a?8
构造器中赋a的值为6

I doubt that point, I started to think that I define in the second block of code and variables of the same name is the same before a (not yet) so no problem, after the initialization code, the implementation of the constructor, call a a, then the time that a call is what it, so have doubts, want to know a small partner can over to help me .

  • When I tested also met ilegal forward reference, namely forward misquoted, as shown below.
    {
        age = 50;
        if(age>40) System.out.println("Father类的初始化块且age>40");
        System.out.println("Father类的第一个初始化块");
    }
    int age =20;

The reason: because when the variable is not defined yet, you quoted it, so in order to avoid such a mistake, as far as possible on after initialization block member variable declarations.

Static initialization block

It is normal and the corresponding static initialization block it, i.e. a static modification, also referred to as a class initialization block . Analysis by name, class initialization block is responsible for the class is initialized, and is responsible for implementing common initialization block initialized object.

  • In static{}the initialization of the surround format class variables.
  • Because the static initialization block and the relevant class, responsible for the class is initialized, so always have to execute than normal initialization block.
  • Typically used to perform class initialization process variables, instance variables can not initialization process.
  • Static initialization block belongs to a static class member, the static member can not follow the rules of non-access static members: i.e. not access non-static member (instance variables and instance methods) .
  • Similarly, the static initialization block and specifies the initial value declared static member variable of the class initialization code time.

Initialization block constructor

On initialization block constructor and has the calling sequence , binding code to be understood at once.

package com.my.pac17;

/**
 * @auther Summerday
 */
public class A {
    {
        System.out.println("A.instance initializer");
    }
    static {
        System.out.println("A.static initializer");
    }
    public A() {
        System.out.println("A.A");
    }
}
class B extends A {
    {
        System.out.println("B.instance initializer");
    }
    static {
        System.out.println("B.static initializer");
    }
    public B() {
        System.out.println("B.B");
    }
    public B(String m) {
        this();
        System.out.println("B.B," + m);
    }
}
class C extends B {
    {
        System.out.println("C.instance initializer");
    }
    static {
        System.out.println("C.static initializer");
    }
    public C() {
        super("ccc");
        System.out.println("C.C");
    }
}
class BTest {
    public static void main(String[] args) {
        new C();
        System.out.println("*******");
        new C();
    }
}
/*测试结果*/
A.static initializer
B.static initializer
C.static initializer
/*类初始化阶段,限制性最顶层父类的静态初始化块
然后依次向下,直到执行当前类的静态初始化块*/
A.instance initializer
A.A
B.instance initializer
B.B//调用B的无参构造器
B.B,ccc//调用B的带参构造器
C.instance initializer//最后执行C
C.C
/*对象初始化阶段,先执行最顶层父类的初始化块,
最顶层父类的构造器,然后依次向下,直到执行当前
类的初始化块、当前类的构造器*/
*******
//不用执行静态初始化语句
A.instance initializer
A.A
B.instance initializer
B.B
B.B,ccc
C.instance initializer
C.C
  • static modification of the static initialization block, always the first to be called , and in inheritance, the static initialization block earliest parent class executed first.
  • You can see, when you create a subclass object a second time, there is no longer performs static initialization block initialized because three classes have been loaded successfully .
  • Common initialization block execution order and is configured to, prior to performing normal initialization block constructor, from the earliest to the current class has a parent class.

Guess you like

Origin www.cnblogs.com/summerday152/p/12075028.html