Summary: static code block is always executed first.
Non-static block of code, like non-static method, with the object concerned. Only non-static block of code before the constructor.
Parent non-static block, the constructor is finished (corresponding parent class object initialization is completed), it starts executing code blocks and non-static constructors subclasses.
================================================================================
The same point: all constructors and before execution in the JVM loads class can be defined in a plurality of classes,
Usually some static variables in the code block assignment.
Different points: a static block of code before the code block non-static
( Static block of code -> non-static block -> Constructor ).
Static block of code is executed only when the first class loader is loaded once and then not performed, rather than static code block is executed once every time new . Non-static block may be defined (but little effect) in the conventional method; and not a static block.
JVM executes the static loading class code block, if a plurality of static block, the JVM sequentially executes them in the order they appear in the class , each code block is executed only once.
Example 1:
-
public class PuTong {
-
public PuTong(){
-
. System out.print ( "default constructor ->!");
-
}
-
-
// non-static block
-
{
-
. The System Out.print ( "non-static block ->!");
-
}
-
-
// static code block
-
static{
-
. The System Out.print ( "static block of code ->!");
-
}
-
-
public static void test(){
-
{
-
. The System Out.println ( "conventional method of code blocks!");
-
}
-
}
-
}
-
-
// test class
-
public class TestClass {
-
-
/**
-
* The difference between the two new static and non-static implementation of block
-
*/
-
public static void main(String[] args) {
-
PuTong c1 = new PuTong();
-
c1.test();
-
-
PuTong c2 = new PuTong();
-
c2.test();
-
}
-
}
-
-
/*
-
Run output is:
-
Static code block! -> non-static block of code! -> default constructor! -> conventional method of code blocks!
-
Non-static block of code! -> default constructor! -> conventional method of code blocks!
-
*/
There are two main points:
1, the parent class, subclass of non-static initialization block when?
2, which is the parent class method when calling overridden method, in the end executed?
Example 2:
-
package tags;
-
-
public class Child extends Father{
-
-
static {
-
System. out.println("child-->static");
-
}
-
-
private int n = 20;
-
-
{
-
System. out.println("Child Non-Static");
-
n = 30;
-
}
-
-
public int x = 200;
-
-
public Child() {
-
this("The other constructor");
-
System. out.println("child constructor body: " + n);
-
}
-
-
public Child(String s) {
-
System. out.println(s);
-
}
-
-
public void age() {
-
System. out.println("age=" + n);
-
}
-
-
public void printX() {
-
System. out.println("x=" + x);
-
}
-
-
public static void main(String[] args) {
-
new Child().printX();
-
}
-
}
-
-
class Father {
-
-
static {
-
//System.out.println("n+"+n);
-
// When n is defined at the following will be prompted Can not reference a field before it is defined,
-
// must be moved n are as defined above, the output can
-
System. out.println("super-->static");
-
}
-
-
public static int n = 10;
-
public int x = 100;
-
-
public Father() {
-
System. out.println("super's x=" + x);
-
age();
-
}
-
-
{
-
System. out.println("Father Non-Static");
-
}
-
-
public void age(){
-
System. out.println("nothing");
-
}
-
}
result:
super-->static
child-->static
Father Non-Static
super's x=100
age=0
Child Non-Static
The other constructor
child constructor body: 30
x=200
Parent class static block of code -> subclass static code block
-> parent class non-static block -> parent class constructor
-> non-static block subclass -> subclass constructor
java, when using the new operator time to create an instance of a class, and the space assigned starting member variable is initialized to the default value , this does not mean to note variable is initialized to an initial value at a variable definition, but to shaping assigned 0, a null string assigned to this different C ++, (student.name = null, student.age = 0)
Then enter the class constructor.
In the constructor function, we must first check whether this or super call , this call is completed calls between the present class constructor itself, super call is called through the parent class. Both can appear only one, and only as an occurrence of the constructor. Jump to realize the program when calling this and super and instead perform this constructor is called or the super constructor.
In this and finished super, variable initialization program executed instead work undertaken in the class definition.
This is finished, the constructor is executed in the rest of the code.