LinkedList data structure, why do we need to call the empty default constructor from another constructor

There java.util.LinkedList

are 2 default and parameterized constructors in the class that accept a collection. When I looked at the implementation, I can see that the parameterized constructor has this()

basically calls the default constructor.

But the default constructor is empty. So I was just curious if this is the best way to call the default constructor even if it's empty or there are some considerations behind it. What happens if we don't call the default constructor.

Inserting GrepCode LinkedList

+3


source to share


4 answers


It simply means that any form, upon initialization, LinkedList

must first follow the default initialization steps.



This may seem strange to you since there are no default initialization steps at the moment (but may appear later).

+2


source


This is not a recognized good practice. Perhaps the default constructor did something in previous versions. Maybe the developer thought it would be a good idea, so if something extra is done in the default constructor, it will also be done elsewhere. If you look at the ArrayList, you can see that a different choice has been made.

What happens if we don't call the default constructor.



The default constructor is simply not called. The no-arg constructor of the superclass is called implicit.

+2


source


If the default constructor is extended at any time in the future, the other constructor does not need to be changed.

It tells us that the extended constructor is basically the default constructor , but with some special features added (for convenience).

+1


source


The parameterized version must create the list in the same way as the non-parameterized one, and call the method for adding items additionally. The fact that the default constructor is empty is implementation specific.

/**
 * Constructs an empty list.
 */
public LinkedList() {
}

      

The fact that he intended to build it this way remains.

+1


source







All Articles