前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java构造函数调用顺序问题

Java构造函数调用顺序问题

作者头像
用户7886150
修改2021-04-20 10:14:39
1.1K0
修改2021-04-20 10:14:39
举报
文章被收录于专栏:bit哲学院

参考链接: Java构造函数

今天对Java的构造函数调用顺序进行研究,使用的是与C++类似的方法,即不对源码进行研究,而是直接通过打印代码对构造函数的调用顺序进行研究。 

代码如下,使用的是Java核心技术中的代码,对其进行了改造,在构造函数中加入了输出信息 

public class ConstructorTest

{

   public static void main(String[] args)

   {

    Employee temp = new Employee();

   }

}

class Employee

{

   private static int nextId;

   private static int counter;

   private int id;

   private String name = ""; // instance field initialization

   private double salary;

   // static initialization block

   static

   {

      Random generator = new Random();

      // set nextId to a random number between 0 and 9999

      nextId = generator.nextInt(10000);

      System.out.println(" static initialization block : " + counter++);

   }

   // object initialization block

   {

      id = nextId;

      nextId++;

      System.out.println(" object initialization block : " + counter++);

   }

   // three overloaded constructors

   public Employee(String n, double s)

   {

      name = n;

      salary = s;

      System.out.println(" constructors1 : " + counter++);

   }

   public Employee(double s)

   {

      // calls the Employee(String, double) constructor

      this("Employee #" + nextId, s);

      System.out.println(" constructors2 : " + counter++);

   }

   // the default constructor

   public Employee()

   {

      // name initialized to ""--see above

      // salary not explicitly set--initialized to 0

      // id initialized in initialization block

       System.out.println(" constructors3 : " + counter++);

   }

}

运行结果如下: 

 static initialization block : 0

 object initialization block : 1

 constructors3 : 2

 通过上述程序验证了:所有数据域被初始化为默认值(0,false,NULL),此处counter初始化为0。 

再来看第二条规则:按照类声明中出现的次序,依次执行所有域初始化语句和初始化块。 

程序运行结果显示先执行静态初始化块后执行域初始化块,可以通过调换上述两个初始化块的次序对这一规则进行验证。 

这里书中的讲解并不是很清楚,静态初始化块的优先级要高于域初始化块,因此静态初始化块的执行要早于域初始化块,只有在同级别的情况下,才按照声明的顺序调用,这一点我通过将static去掉进行了验证。 

代码: 

// object initialization block

   {

      id = nextId;

      nextId++;

      System.out.println(" object initialization block : " + counter++);

   }

   // static initialization block

   {

      Random generator = new Random();

      // set nextId to a random number between 0 and 9999

      nextId = generator.nextInt(10000);

      System.out.println(" static initialization block : " + counter++);

   }

  执行结果 

 object initialization block : 0

 static initialization block : 1

 constructors3 : 2

  构造函数最后调用,没有什么问题。 

最后一点:如果构造器第一行调用了第二个构造器,则执行第二个构造器的主体。 

最后还有一点非常重要的内容,构造器调用一定要是第一句,否则编译出错。在构造器中只能调用一次其他构造函数,不能调用两次,即无法再调用第三个构造函数。 

本人是初学者,还无法从JVM的角度分析问题,同时回应各位大神对文中的错漏进行指出。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档