0

I am wondering whether I should initialize an ArrayList inside a constructor or outside, as demonstrated below. Is there any difference between the two ways or if not, what do you think is the most common/best way of doing it in the industry?

Initializing the variable inside the constructor:

public class Customer {
    private String name;
    private ArrayList<Double> transactions;

    public Customer(String name, double initialAmount) {
        this.name = name;
        this.transactions = new ArrayList<Double>();
    }

    public void addTransaction(double amount) {
        this.transactions.add(amount); // autoboxing
    }
}

vs outside:

public class Customer {
    private String name;
    private ArrayList<Double> transactions = new ArrayList<Double>();

    public Customer(String name, double initialAmount) {
        this.name = name;
    }

    public void addTransaction(double amount) {
        this.transactions.add(amount); // autoboxing
    }
}
shadowByte
  • 93
  • 1
  • 1
  • 6
  • Literally the only difference is that name and transactions would be initialized in a different order: name is first in the first case; transactions is first in the second case, because the initializers are automatically inserted into all constructors invoking `super` (explicitly or implicitly), immediately after the call to `super`. – Andy Turner Feb 29 '20 at 19:11
  • 1
    Does this answer your question? [Should I instantiate instance variables on declaration or in the constructor?](https://stackoverflow.com/questions/1994218/should-i-instantiate-instance-variables-on-declaration-or-in-the-constructor) – kasptom Feb 29 '20 at 19:14

2 Answers2

1

There is no difference between the two methods. If you were working in an activity or fragment class then it would be best to initialize a variable inside onCreate. The only advantage of initializing it outside constructor in such a scenario would be if other functions need to load before onCreate is called.

But in a situation of a class, then there is no difference is by the preference of the coder.

Axes Grinds
  • 756
  • 12
  • 24
1

Initializing a variable inside the constructor is the better practice and more common as well. Declare all variables outside the constructor and initialize them inside. Suppose you want to initialize the size of the array named cart in the Customer class. Each customer certainly isn't going to buy the same amount of items as the other. If you wrote your class like this:

public class Customer {
    private String name;
    private Item[] cart=new Item[5];

    public Customer(String name) {
        this.name = name;
    }

}

Then you have set the number of bought items of all customers to 5. On the other hand, by writing the class like this:

public class Customer {
        private String name;
        private Item[] cart;

        public Customer(String name,int size) {
            this.name = name;
            cart=new Item[size];
        }
    }

This implementation ensures that each customer has his own cart of a particular size according to what is passed to the argument size in the constructor.