Common Mistakes I found in intern developers: Part 2: Constructors

Dulaj Rajitha
3 min readMay 28, 2019
https://www.maxpixel.net/

With the experience I got by working with software engineering interns, I noticed some of common mistakes and programming patterns.

Here I have listed down some of common mistakes and bad practices which will be done by new software developers. Therefore this will help the new java developers to identify and correct their mistakes in future.

This article is the second of the series.

The second most common mistakes in model Classes are happen in the class Constructors.

No default constructor where collection fields existing

When there is a class with collections, it’s best practice to have a constructor with collection initialization. Otherwise the default value of the collection is null if the default constructor is called.

But for primitive fields, the practice is to instantiate them inline and not in the constructor with the default value.

/**
* Created by Dulaj on 2019-05-28.
*/
public class DataHolder<T> {

private String id = "";
private List<T> data;

public DataHolder() {
data = new ArrayList<>();
}

public List<T> getData() {
return data;
}

public void setData(List<T> data) {
this.data = data;
}
}

Calling non final methods in constructors

Calling non final methods will lead to unexpected behaviors, because in child classes that non final method can be overridden. Therefore the behavior may not be same as expected. (calling the super method will call overridden child method and that can cause errors). External Example

The reason for those issues will mainly because if overridden method access a child class field that will be initialized in the constructor. But calling public final methods or private methods (both cannot be overridden) will never cause such issues and safe to call in the constructor.

Constructor/ Method with large number of parameters

Method with large number of parameters will lead to less readability and understandability of the code. Therefore the constructor and other methods should have limited number of parameters. If some method needs a lots parameters, then we have to encapsulate them (using an additional class).

Utility classes need a private constructor

Util Class is a class with only static members (methods and fields).

Basically, a utility class without a private constructor will lead to create an instance by mistake and hides the util nature (not meant to create instances). But the whole point of having a Util class is to call its methods using the class (not by the instance). Java will add a default constructor to every class if not declared explicitly. Therefore it’s required to have a private constructor to avoid instantiation.

Above example shows a class with only static members and the private constructor will make sure not to create instances outside.

Then you can call methods and access the fields like this.

int length = SampleUtil.getLength("sample text ");
String key = SampleUtil.KEY;

Let’s see some generic mistakes in the part three of the article series.

Please leave a comment below if you have any questions or feedback.

If you like this post, follow me on Medium for more similar posts.

If you have any concerns or questions, please use the comment section below.

--

--