对于java类,在构造函数中使用“new”和在类外使用“new”之间有什么区别?

时间:2023-01-15 17:43:21

For the java class, what are the differences between using "new" inside the constructor and using "new" outside the class? Take an example as follow,

对于java类,在构造函数中使用“new”和在类外使用“new”之间有什么区别?举个例子如下,

abstract class PowerStat{
    final int numOfComponent = UserProperty.numOfComponent;
    final int windowSize = 8;

    CircularFifoQueue<ArrayList<Double>> movingEnergy;
    CircularFifoQueue<Double> movingStartTimes;
    CircularFifoQueue<Double> movingEndTimes;

    private double [] maxPower = new double[numOfComponent];
    private double [] minPower = new double[numOfComponent];

    public ArrayList<Double> intervalEnergy = new ArrayList<Double>(numOfComponent);

    private ArrayList<UsageNode> usageList = new ArrayList<UsageNode>();

    public PowerStat(){
        setUsageList(new ArrayList<UsageNode>());
        for (int i = 0; i < numOfComponent; i++) {
            intervalEnergy.add(0.0);
        }

        movingEnergy = new CircularFifoQueue<ArrayList<Double>>(windowSize);
        movingStartTimes = new CircularFifoQueue<Double>(windowSize);
        movingEndTimes = new CircularFifoQueue<Double>(windowSize);
    }
}

maxPower is created in the class outside the constructor. However, movingEnergy is instantiated inside the constructor. What are the differences between these two methods.

maxPower是在构造函数外部的类中创建的。但是,movingEnergy在构造函数中实例化。这两种方法有什么不同。

4 个解决方案

#1


3  

The new operations for the fields (outside the constructor) are executed before the constructor.

字段的新操作(构造函数外部)在构造函数之前执行。

#2


0  

The keyword new allocates memory in the size of the object you are creating. For example int[] arr = new int[5]; will allocate 5 * 4 bytes of memory for arr.

关键字new以您正在创建的对象的大小分配内存。例如int [] arr = new int [5];将为arr分配5 * 4字节的内存。

There is no difference where you are doing it.

你在做什么没有区别。

#3


0  

private double [] maxPower = new double[numOfComponent];

Creates a new double array with numOfComponent number of elements.

使用numOfComponent元素数创建一个新的double数组。

movingEnergy = new CircularFifoQueue<ArrayList<Double>>(windowSize);

Here, new keyword calls CircularFifoQueue constructor, thus creating a new CircularFifoQueue object and assigning it to movingEnergy variable

这里,new关键字调用CircularFifoQueue构造函数,从而创建一个新的CircularFifoQueue对象并将其分配给movingEnergy变量

If you are interested in the order of execution, see Are fields initialized before constructor code is run in Java?.

如果您对执行顺序感兴趣,请参阅在Java中运行构造函数代码之前是否已初始化字段?

  1. Static variable initialisers and static initialisation blocks, in textual order, if the class hasn't been previously initialised.
  2. 静态变量初始化和静态初始化块,以文本顺序排列,如果该类先前尚未初始化。
  3. The super() call in the constructor, whether explicit or implicit.
  4. 构造函数中的super()调用,无论是显式调用还是隐式调用。
  5. Instance variable initialisers and instance initialisation blocks, in textual order.
  6. 实例变量初始化和实例初始化块,按文本顺序排列。
  7. Remaining body of constructor after super().
  8. super()之后的剩余构造函数体。

See sections §2.17.5-6 of the Java Virtual Machine Specification.

请参阅Java虚拟机规范的第2.17.5-6节。

#4


0  

Logically, the compiler rearranges your code to the following code. It adds the super() call you didn't specify, and moves all the initializers into the constructor.

从逻辑上讲,编译器会将您的代码重新排列为以下代码。它添加了您未指定的super()调用,并将所有初始化程序移动到构造函数中。

As a result, there is really no difference.

结果,确实没有区别。

Initializing fields in the constructor will however allow you to use constructor parameters and/or intermediate calculations. If you don't need that, it makes no difference whether you initialize the field on the field declaration or in the constructor.

但是,在构造函数中初始化字段将允许您使用构造函数参数和/或中间计算。如果您不需要,那么无论是在字段声明上还是在构造函数中初始化字段都没有区别。

As for exact order object object initialization, see this answer: https://*.com/a/23094875/5221149

至于确切的订单对象对象初始化,请参阅以下答案:https://*.com/a/23094875/5221149

abstract class PowerStat{
    final int numOfComponent;
    final int windowSize;

    CircularFifoQueue<ArrayList<Double>> movingEnergy;
    CircularFifoQueue<Double> movingStartTimes;
    CircularFifoQueue<Double> movingEndTimes;

    private double [] maxPower;
    private double [] minPower;

    public ArrayList<Double> intervalEnergy;

    private ArrayList<UsageNode> usageList;

    public PowerStat(){
        super();
        this.numOfComponent = UserProperty.numOfComponent;
        this.windowSize = 8;
        this.maxPower = new double[this.numOfComponent];
        this.minPower = new double[this.numOfComponent];
        this.intervalEnergy = new ArrayList<Double>(this.numOfComponent);
        this.usageList = new ArrayList<UsageNode>();

        setUsageList(new ArrayList<UsageNode>());
        for (int i = 0; i < numOfComponent; i++) {
            intervalEnergy.add(0.0);
        }

        movingEnergy = new CircularFifoQueue<ArrayList<Double>>(windowSize);
        movingStartTimes = new CircularFifoQueue<Double>(windowSize);
        movingEndTimes = new CircularFifoQueue<Double>(windowSize);
    }
}

#1


3  

The new operations for the fields (outside the constructor) are executed before the constructor.

字段的新操作(构造函数外部)在构造函数之前执行。

#2


0  

The keyword new allocates memory in the size of the object you are creating. For example int[] arr = new int[5]; will allocate 5 * 4 bytes of memory for arr.

关键字new以您正在创建的对象的大小分配内存。例如int [] arr = new int [5];将为arr分配5 * 4字节的内存。

There is no difference where you are doing it.

你在做什么没有区别。

#3


0  

private double [] maxPower = new double[numOfComponent];

Creates a new double array with numOfComponent number of elements.

使用numOfComponent元素数创建一个新的double数组。

movingEnergy = new CircularFifoQueue<ArrayList<Double>>(windowSize);

Here, new keyword calls CircularFifoQueue constructor, thus creating a new CircularFifoQueue object and assigning it to movingEnergy variable

这里,new关键字调用CircularFifoQueue构造函数,从而创建一个新的CircularFifoQueue对象并将其分配给movingEnergy变量

If you are interested in the order of execution, see Are fields initialized before constructor code is run in Java?.

如果您对执行顺序感兴趣,请参阅在Java中运行构造函数代码之前是否已初始化字段?

  1. Static variable initialisers and static initialisation blocks, in textual order, if the class hasn't been previously initialised.
  2. 静态变量初始化和静态初始化块,以文本顺序排列,如果该类先前尚未初始化。
  3. The super() call in the constructor, whether explicit or implicit.
  4. 构造函数中的super()调用,无论是显式调用还是隐式调用。
  5. Instance variable initialisers and instance initialisation blocks, in textual order.
  6. 实例变量初始化和实例初始化块,按文本顺序排列。
  7. Remaining body of constructor after super().
  8. super()之后的剩余构造函数体。

See sections §2.17.5-6 of the Java Virtual Machine Specification.

请参阅Java虚拟机规范的第2.17.5-6节。

#4


0  

Logically, the compiler rearranges your code to the following code. It adds the super() call you didn't specify, and moves all the initializers into the constructor.

从逻辑上讲,编译器会将您的代码重新排列为以下代码。它添加了您未指定的super()调用,并将所有初始化程序移动到构造函数中。

As a result, there is really no difference.

结果,确实没有区别。

Initializing fields in the constructor will however allow you to use constructor parameters and/or intermediate calculations. If you don't need that, it makes no difference whether you initialize the field on the field declaration or in the constructor.

但是,在构造函数中初始化字段将允许您使用构造函数参数和/或中间计算。如果您不需要,那么无论是在字段声明上还是在构造函数中初始化字段都没有区别。

As for exact order object object initialization, see this answer: https://*.com/a/23094875/5221149

至于确切的订单对象对象初始化,请参阅以下答案:https://*.com/a/23094875/5221149

abstract class PowerStat{
    final int numOfComponent;
    final int windowSize;

    CircularFifoQueue<ArrayList<Double>> movingEnergy;
    CircularFifoQueue<Double> movingStartTimes;
    CircularFifoQueue<Double> movingEndTimes;

    private double [] maxPower;
    private double [] minPower;

    public ArrayList<Double> intervalEnergy;

    private ArrayList<UsageNode> usageList;

    public PowerStat(){
        super();
        this.numOfComponent = UserProperty.numOfComponent;
        this.windowSize = 8;
        this.maxPower = new double[this.numOfComponent];
        this.minPower = new double[this.numOfComponent];
        this.intervalEnergy = new ArrayList<Double>(this.numOfComponent);
        this.usageList = new ArrayList<UsageNode>();

        setUsageList(new ArrayList<UsageNode>());
        for (int i = 0; i < numOfComponent; i++) {
            intervalEnergy.add(0.0);
        }

        movingEnergy = new CircularFifoQueue<ArrayList<Double>>(windowSize);
        movingStartTimes = new CircularFifoQueue<Double>(windowSize);
        movingEndTimes = new CircularFifoQueue<Double>(windowSize);
    }
}