[Java] Static code block usage, java static code

Source: Internet
Author: User

[Java] Static code block usage, java static code

I. Differences between java static code blocks and static methods
Generally, if some code must be executed when the project is started, a static code block is required. This code is actively executed and needs to be initialized when the project is started, when other programs call an object without creating an object, a static method is required. Such code is passively executed. static methods can be called directly by class name when they are loaded.
For example, the main method must be static. This is the program entry.
The difference between the two is that static code blocks are automatically executed;
Static methods are executed only when they are called.

Static Method
(1) in Java, you can define a method that does not need to create an object. This method is a static method. To achieve this effect, you only need to add the static keyword before the method defined in the class. For example:

Public static int maximum (int n1, int n2)

Note the following when using static methods of the class:
1. In a static method, only other static members (including variables and methods) of the same type can be called directly, rather than non-static members in the category. This is because for non-static methods and variables, you need to create an instance object for the class before use, and the static method does not need to create any objects before use.

2. static methods cannot reference the this and super keywords in any way, because static methods do not need to create any instance objects before they are used. When static methods are called, the objects referenced by this are not generated at all.

(2) static variables belong to the entire class instead of an object. Note that the variables in any method body cannot be declared as static. For example:
Fun ()
{
Static int I = 0; // invalid.
}

(3) A class can use a static code block that is not included in any method body. When the class is loaded, the static code block is executed and only executed once, static blocks are often used to initialize class attributes. For example:
Static
{
}

Class loading steps
In Java, the Class Loader loads a class into a Java Virtual Machine. Three steps are required: Loading, linking, and initialization, the link can be divided into three steps: verification, preparation, and resolution. In addition to resolution, the other steps are completed in strict order. The main work of each step is as follows:

Load: Query and import binary data of classes or interfaces;
Link: perform the following verification, preparation, and resolution steps. The resolution steps are optional;
Verify: Check whether the binary data of the import class or interface is correct;
Preparation: allocate and initialize the storage space for static variables of the class;
Resolution: convert a symbolic reference to a direct reference;
Initialization: Initialize the Java code and static Java code block for activating static variables of the class.
Attributes in the initialization class are commonly used for static code blocks, but can only be used once.

Ii. initialization sequence of static code blocks

Public class Parent {static String name = "hello"; {System. out. println ("parent block");} static {System. out. println ("parent static block");} public Parent () {System. out. println ("parent constructor") ;}} public class Child extends Parent {static String childName = "hello"; static {System. out. println ("child static block");} public Child () {System. out. println ("child constructor");} {System. out. println ("child block");} public static void main (String [] args) {new Child (); // Statement 1 }}

When Statement 1 is executed, what is the order of the printed results?
Print result:

Parent static block
Child static block
Parent block
Parent constructor
Child block
Child constructor

Analysis:

When new Child () is executed, it first checks whether there is a static code block in the parent class. If yes, it first executes the content in the static code block in the parent class, after the content in the static code block of the parent class is executed, execute the static code block in the subclass (own class). After the static code block of the subclass is executed, it then checks whether the parent class has non-static code blocks. If yes, it executes non-static code blocks of the parent class. The non-static code blocks of the parent class are executed, and then the construction method of the parent class is executed; after the constructor of the parent class is executed, it goes on to check whether the Child class has non-static code blocks. If so, it executes the non-static code blocks of the Child class. After the non-static code block of the subclass is executed, the constructor of the subclass is executed. This is the initialization sequence of an object.

Summary:
Object initialization sequence: first, execute the static content of the parent class. After the static content of the parent class is executed, execute the static content of the Child class. After the static content of the Child class is executed, check whether the parent class has non-static code blocks. If yes, execute the non-static code blocks of the parent class. After the non-static code blocks of the parent class are executed, execute the construction method of the parent class; after the constructor of the parent class is executed, it goes on to check whether the Child class has non-static code blocks. If so, it executes the non-static code blocks of the Child class. The non-static code block of the subclass is executed before the subclass constructor is executed. In a word, the content of the static code block is first executed, followed by the non-static code block and constructor of the parent class, and then the non-static code block and constructor of the subclass are executed.

 

Note:

The constructor of the subclass. no matter whether the constructor includes any parameters, it will first find the constructor without parameters of the parent class by default. If the parent class does not have a constructor without parameters, the subclass must use the supper key child to call the constructor with parameters in the parent class. Otherwise, the compilation fails.

 

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.