Reprinted from:
Http://blog.sina.com.cn/s/blog_afddb8ff0101aqs9.html
Static code blocks: Some code must be executed at the start of the project , which is executed proactively (when the class is loaded, the static block is executed and executed only once, and static blocks are often used to perform initialization of class properties )
The difference between the two is: static code blocks are automatically executed; static methods are executed only when they are called.
When using static methods of a class, note that:
A, in a static method can only directly invoke other static members of the same class (including variables and methods), and not directly access non-static members of the classes ( This is because, for non-static methods and variables, you need to create an instance object of the class before it can be used, Static methods do not have to create any objects before they are used
B. static methods cannot refer to this and the Super keyword 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.
C, Static variables are variables that belong to the entire class and not to an object. Note that you cannot declare a variable in any method body as static
Class loading Order in Java
1. When the virtual machine loads the Java class for the first time, it initializes the static initialization block, static member variable, static method one time 2, only when the new method is called to create instance 3 of the class, the class instance creation process: Initializes according to the parent-child inheritance, first executes the initialization block part of the parent class, Then the constructor of the parent class, and then the initialization block of the subclass that inherits from this class, and finally the subclass constructor Method 4, the class instance destroys, first destroys the subclass part, then destroys the parent class part
Summary: Object initialization order: first executes the parent class static content, after the parent class static content executes, then executes the subclass the static content, when the subclass static content executes, then goes to see the parent class has the non-static code block, if has executes the parent class non-static code block, the parent class's non-static code block executes completes , then executes the parent class's construction method, and after the parent class's construction method executes, it goes on to see if the subclass has no non-static code block, and if there is one, executes the subclass's non-static code block. 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.
Java Foundation--java Static code block and static method difference, static usage