2

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,

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.

4 Answers4

3

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

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • When do the fields(outside the constructor) execute?. When the Powerstat class isn't instantiated, is maxPower' space created? Each instance has its own maxPower space? – 80247617 aaa Mar 02 '16 at 18:27
  • The variables are instance variables. They are only created when a constructor is called; then they are filled before the constructor is executed. You can consider them as invisible part of every constructor. – J Fabian Meier Mar 02 '16 at 18:29
  • This answer is not technically true. If the constructor had a call to `super()`, the field initializers would execute *after* the call the `super()`, but before the rest of the constructor body. – Andreas Mar 02 '16 at 18:31
  • That is true. I did not discuss super() constructors. – J Fabian Meier Mar 02 '16 at 18:35
  • 1
    See this answer for detail of object initialization order: http://stackoverflow.com/a/23094875/5221149 – Andreas Mar 02 '16 at 18:36
1

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.

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://stackoverflow.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);
    }
}
Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
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.

There is no difference where you are doing it.

Guy
  • 46,488
  • 10
  • 44
  • 88
0
private double [] maxPower = new double[numOfComponent];

Creates a new double array with numOfComponent number of elements.

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

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

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

  1. Static variable initialisers and static initialisation blocks, in textual order, if the class hasn't been previously initialised.
  2. The super() call in the constructor, whether explicit or implicit.
  3. Instance variable initialisers and instance initialisation blocks, in textual order.
  4. Remaining body of constructor after super().

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

Community
  • 1
  • 1
radoh
  • 4,554
  • 5
  • 30
  • 45