代码块
定义:使用{}定义的一段代码
在Java中,根据代码块出现的位置以及关键字不同,分为以下四类
1.普通代码块
2.构造块(重点)
3.静态代码块
4.同步代码块
普通代码块
在方法或语句中出现{}就是普通代码块,比如下图,可以发现x定义了两次,由于在不同的代码块中,不会出现重定义的问题。
public class Test{
public static void main(String[] args) {
{
//普通代码块
int x=3;
System.out.println(x);
}
{
//普通代码块
int x = 4;
System.out.println(x);
}
}
}
构造块
定义在类中的代码块 (不加任何修饰符)
当有对象产生时,构造块优先于构造方法执行,产生几个对象就调用几次构造块
作用:在构造方法执行之前进行一些初始化操作
public class Test{
public static void main(String[] args) {
new Person();
}
}
//Person类
class Person{
//Person的构造方法
public Person(){
System.out.println("1.person的构造方法");
}
{
//构造块
System.out.println("2.person的构造块");
}
}
输出
2.person的构造块
1.person的构造方法
静态代码块
在类中使用static定义的代码块
public class Test{
public static void main(String[] args) {
new Person();
new Person();
}
}
//Person类
class Person{
public Person(){
System.out.println("1.person的构造方法");
}
{
//构造块
System.out.println("2.person的构造块");
}
static{
//静态代码块
System.out.println("3.person的静态块");
}
}
输出结果是
3.person的静态块
2.person的构造块
1.person的构造方法
2.person的构造块
1.person的构造方法
可见静态代码块在类加载时(或者说主方法中使用类时)被调用,优先于构造块,而且不管产生多少实例化对象,只会调用一次
这是非主类的静态代码块,如果静态代码块写在主类中呢?
public class Test{
static{
System.out.println("1 主类的静态块 ");
}
public Test(){
System.out.println("2 主类的构造方法");
}
{
System.out.println("3 主类的构造块");
}
public static void main(String[] args) {
System.out.println("4主函数开始。。。");
new Test();
new Test();
System.out.println("5主函数结束。。。");
}
}
输出
1 主类的静态块
4主函数开始。。。
3 主类的构造块
2 主类的构造方法
3 主类的构造块
2 主类的构造方法
5主函数结束。。。
可见主类中的静态代码块优先于主方法执行。
下面再看一下有继承关系的2个类中代码块的执行顺序
public class Test{
public static void main(String[] args) {
System.out.println("4主函数开始。。。");
new student();
System.out.println("5主函数结束。。。");
}
}
//Person类
class Person{
public Person(){
System.out.println("1.person的构造方法");
}
{
//构造块
System.out.println("2.person的构造块");
}
static{
//静态代码块
System.out.println("3.person的静态块");
}
}
//student类继承Person类
class student extends Person{
public student(){
System.out.println("6.student的构造方法");
}
{
System.out.println("7.student的构造块");
}
static{
System.out.println("8.student的静态块");
}
}
输出
4主函数开始。。。
3.person的静态块
8.student的静态块
2.person的构造块
1.person的构造方法
7.student的构造块
6.student的构造方法
5主函数结束。。。
可见子类实例化前要调用父类的构造方法产生父类对象后再调用子类构造
终极例子
public class Test{
static{
System.out.println("1.主类的静态块 ");
}
public Test(){
System.out.println("2.主类的构造方法");
}
{
System.out.println("3.主类的构造块");
}
public static void main(String[] args) {
System.out.println("4.主函数开始。。。");
new Test();
new student();
new student();
System.out.println("5.主函数结束。。。");
}
}
//Person类
class Person{
public Person(){
System.out.println("6.person的构造方法");
}
{
//构造块
System.out.println("7.person的构造块");
}
static{
//静态代码块
System.out.println("8.person的静态块");
}
}
class student extends Person{
public student(){
System.out.println("9.student的构造方法");
}
{
System.out.println("10.student的构造块");
}
static{
System.out.println("11.student的静态块");
}
}
输出
1.主类的静态块
4.主函数开始。。。
3.主类的构造块
2.主类的构造方法
8.person的静态块
11.student的静态块
7.person的构造块
6.person的构造方法
10.student的构造块
9.student的构造方法
7.person的构造块
6.person的构造方法
10.student的构造块
9.student的构造方法
5.主函数结束。。。
同步代码块
后期更新。。。