Use initialization block

Java, there are two initialization block: static and non-static initialization block initialization block. They are defined in the class, braces {} enclosed, static code block plus the static keyword outer braces.

The method is suitable for a large number of repeat calls can save resources without the same parameters and methods to avoid repeating a lot of writing

A non-static initialization block:

Here Insert Picture Description

Non-static effect initialization block is to the object, his priority is greater than operating priority general constructor, the difference between him and general constructor is that the non-static initialization block his unified initialization to all objects, constructors only to the corresponding object initialization.

Application: The constructors for all things in common is defined in the configuration block.

2 static initialization block

Here Insert Picture Description

Initialized to the class. With the loaded class is executed, and executed only once

And code block configuration differences:

1) configured for initializing the block objects, each created object will be executed once; static block of code used to initialize class, the class is performed with loading, regardless of creating several objects, only executed once.

2) a static code block structure in preference to execute code block

3) is defined in the class, with the static keyword, without a static

When an object is created, the virtual memory is allocated, mainly used to store an object instance variables inherited from its superclass instance variables come (even if they are inherited from the superclass instance variables over there may be hidden will be allocate space). While the memory allocated for the instance variables, instance variables will be given a default value.

Here Insert Picture Description

The above code, Foo and Bar are defined in the variable i, the main method, we refer to a Bar object with Foo, if the instance variables and methods, since it allows to be covered, then the results printed should be 1, but the actual 0 result indeed. But if i direct method of Bar, then use would be Bar object instance variable i own definition, which is hidden, Bar object i the Foo object i to hide, this rule for static variables The same applies. After the completion of memory allocation, java virtual machine will begin the initialization operation of the newly created object, because the java specifications initialized in reference to an object before it needs to be seen. In Java, the object initialization performed three kinds of structures, respectively instance initializer instance variable is initialized and the constructor.

Initialization block execution order

Static class initialization block parent

Static initialization block subclass

Parent class initialization block

Parent class constructor

Initialization block subclass

Subclass Constructors

Guess you like

Origin blog.csdn.net/weixin_44589117/article/details/89371155