1. The code block appreciated
Enclosed by {} code;
2. The classification block
. A local block - means for defining a variable life cycle, early release, improved memory utilization. With the execution of the method is performed;
B block structure - a plurality constructor same code can be placed here, a constructor before each execution block configured to execute first;.
. C static block of code - action It can be performed once the initial exchange data classes, the class loading time;
3. execution order
Static block of code> configuration block> partial block
4. execution order demo
class Student{
//静态代码块
static{
System.out.println("我是student的静态代码块");
}
//构造代码块
{
System.out.println("student的构造代码块");
}
public Student(){
System.out.println("student的无参构造");
}
public Student(int a){
System.out.println("student的有参构造");
}
public void show(){
{
System.out.println("student的局部代码块");
}
}
}
class Test{
public static void main(String[] args){
Student student1 = new Student();
Student student2 = new Student(1);
student2.show();
}
}
The results:
I am a student of the static code block
construction block student's
constructor without parameters student of
student construction block
student has constructed a reference
partial block of student