The use of static code blocks in Java--static usage

Source: Internet
Author: User

(i)Java static code block static method differences
In general, if some code must be executed at the time of project startup, the use of static code blocks, the code is active, it needs to be initialized when the project is started, in the case of not creating the object, other programs to invoke, the need to use a static method, the code is passive execution. Static methods are loaded when the class is loaded and can be called directly with the class name
For example, the main method must be static, which is the program entry.
The difference between the two is: static code blocks are automatically executed;
Static methods are executed only when they are called.

Static methods
(1) In Java, you can define a method that does not need to create an object, which 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)

When using static methods of a class, note that:

A static method can only invoke other static members (including variables and methods) directly in the same class, and cannot directly access non-static members in the classes. This is because, for non-static methods and variables, you need to create an instance object of the class before you can use it, and the static method does not have to create any objects before it is used.

b Static methods cannot reference the this and Super keywords in any way, because static methods do not have to create any instance objects before they are used, and when a static method is called, the object referenced by this is not produced at all.

(2) A static variable is a variable that belongs to the entire class and not to an object. Note that variables in any method body cannot be declared static, for example:
Fun ()
static int i=0;//illegal.

(3) A class can use a static block of code that is not contained in any method body, when the class is loaded, a static code block is executed, and only once, static blocks are used to perform initialization of class properties. For example:

Class load steps
In Java, the class loader to load a class into a Java virtual machine, to go through three steps to complete: loading, linking and initialization, wherein the link can be divided into the verification, preparation and parsing three steps, in addition to parsing, the other steps are strictly in order to complete, the main work of each step is as follows:

Loading: Finding and importing binary data for classes or interfaces;
Link: Perform the following validation, preparation and resolution steps, where the parsing step is optional;
Checksum: Check the correctness of binary data of imported class or interface;
Prepare: Allocate and initialize storage space for static variables of class;
Parsing: The symbolic reference is converted to a direct reference;
Initialize: Initializes the Java code and static Java code blocks of the static variables of the class.
Initializing a property in a class is a common use of static code blocks, but only once.

(ii) initialization order of static code blocks

  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 ");  

Class Child extends parent{ 
static String childname = "Hello";  

System.out.println ("Child Block"),  

Static { 
System.out.println ("Child static Block ");  

Public Child () { 
System.out.println (" Child constructor ");  

public class Staticiniblockordertest { 

public static void Main (string[] args) { 
New Child ();//Statement (*)  


Question: What is the order in which the results are printed when the statement (*) finishes executing? Why?
Answer: When you finish executing the statement (*), the printing result is in such a sequence:

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

Analysis: When the new Child () is executed, it first goes to see if there is a static block of code inside the parent class, and if so, it first executes the contents of the static code block inside the parent class, and then executes the static block of code inside the subclass (own class) after the contents of the static code block inside the parent class are executed. When the static code block of a subclass executes, it goes on to see if the parent class has no non-static block of code, and if there is a non-static block of code that executes the parent class, the non-static code block of the parent class is executed, and the parent class's constructor is executed, and then it goes to see if the subclass has non-static A non-static block of code that executes subclasses if there is one. The non-static code block of the subclass executes and then executes the subclass's constructor, which is the initialization order of an object.

Object Initialization order: first executes the parent class static content, after the parent class static content executes, then executes the subclass static content, when the subclass static content executes completes, then goes to see the parent class has not the non-static code block, if has executes the non-static code block of the parent class, The non-static block of code for the parent class is executed, followed by the constructor of the parent class, and after the construction method of the parent class is executed, it goes on to see if the subclass has no non-static code block, and if there is a non-static block of code that executes the subclass. The non-static code block of the subclass executes and then executes the subclass's construction method. In short, the static code block content executes first, then executes the parent class non-static code block and construction method, then executes the subclass non-static code block and constructs the method.

Note: The constructor of a subclass, regardless of the constructor with no arguments, will first look for the parent class's constructor without parameters. If the parent class does not have a constructor with no arguments, the subclass must use the supper key to call the parent class with the constructor of the parameter, otherwise the compilation cannot pass.

The use of static code blocks in Java--static usage

Related Article

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: 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.