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.
source to share
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.
source to share
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.
source to share