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.

0

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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.
That is true. I did not discuss super() constructors.
See this answer for detail of object initialization order: stackoverflow.com/a/23094875/5221149
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); } } 

1 Comment

I thought The Java compiler copies only initializer blocks into every constructor.
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.

Comments

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.

1 Comment

@Andreas yeah well, the JVM spec contains the most details :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.