80247617 aaa
80247617 aaa

Reputation: 43

For the java class, what are the differences between using "new" inside the constructor and using "new" outside the class?

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.

Upvotes: 2

Views: 94

Answers (4)

Andreas
Andreas

Reputation: 159086

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);
    }
}

Upvotes: 1

radoh
radoh

Reputation: 4809

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.

Upvotes: 0

Guy
Guy

Reputation: 50809

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.

Upvotes: 0

J Fabian Meier
J Fabian Meier

Reputation: 35785

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

Upvotes: 3

Related Questions