0

I have initialized an arraylist like the following:

private ArrayList<Automobile> car = new ArrayList();

Then in the constructor i have the following:

public vehicles() {
car = new ArrayList();
}

Could someone tell me is the second part in the constructor vehicles (car = new ArrayList();) really needed? What does it do?

6 Answers 6

1

You have thrown away a List when you reinitialize car, and you are using Raw Types. Please don't. Use the diamond operator <> instead,

private ArrayList<Automobile> car = new ArrayList<>();

Also, class names should start with a capital letter so vehicles should really be Vehicles (or Vehicle).

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

Comments

1

The initialization order of object creation is

1) initialize fields

2) execute constructor.

In your case, when you create object of vehicles class. First field initialization will happen

private ArrayList<Automobile> car = new ArrayList<>();

then it will execute the constructor.

public vehicles() {
  car = new ArrayList<>();
}

So the reference car will be reinitialized with new ArrayList object.

So you can avoid either field initialization or initialization in constructor.

Comments

1

It doesn't cause any problem to code since there will be only one reference car. car reference will point to the object created by constructor. object created by field will be taken by garbage collector but its unnecessary.

Comments

0

It's not needed. You are initializing the car variable twice. The second initialization (in the constructor) overwrites the reference to the list that was initialized in the declaration of the car member. Therefore you are creating two objects, and one of them is never used, and can be immediately garbage collected (since you don't keep any reference to it).

Comments

0

If you're only declaring the variable, then you need to initialize in your constructor

private ArrayList car;

But if you are initializing it, then you don't need to do it in your constructor

private ArrayList car = new ArrayList();

Comments

0

No its not needed. Same arraylist will get initialized twice instead. So what's going to happen is:

When you will try say instantiating your class, it will first try to create instance which is defined outside your constructor.

Next when your constructor is called you are again overriding that reference which was created earlier with the one that you are creating in constructor.

If you want to enforce that you can't instantiate either knowingly or unknowingly, use final keyword when you define it as a class member. In this way you wont end up creating object in constructor.

Even if you don't instantiate final member variable, compiler wont be happy and will ask you to instantiate it in your default constructor that you created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.