Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit Constructor Invocation using 'this' as poor coding practice?

I was told by a Professor that explicit constructor invocation using this was "poor coding practice" and penalized for it. However, I haven't been able to find anything in any java style guide that I've looked through that comments on it one way or another. On top of that, it seems to be done in quite a bit of coding examples that I have seen. I was hoping to get some input on whether this is poor coding practice and why.

An example of what I am referring to:

public class SomeClass {

    private int a;
    private int b;

    public SomeClass() {
        this(0);
    }

    public SomeClass(int a) {
        this(a, 0);
    }

    public SomeClass(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

EDIT: His comment exactly was "One constructor calling a constructor of the same class is not good practice. A constructor creates an object, so calling a constructor that calls another constructor what is happening in memory? Something to thing about."

And this was the specific code:

public class Employee {
    private String name;
    private int monthlySalary;

    // Default constructor
    public Employee() {
        this("", 0);
    }

    // Constructor
    public Employee(String name, int monthlySalary) {
        this.name = name;
        this.monthlySalary = monthlySalary;
    }

    // annualSalary() method returns the annual salary of the employee as an int
    public int annualSalary() {
        return monthlySalary * 12;
    }

    // toString() method returns the employee name and monthly salary as a 
    // String in the format: name, monthly salary
    public String toString() {
        return "Name: " + name + "\t\tMonthly Salary: " + monthlySalary;
    }
}
like image 773
Nick W Avatar asked May 17 '16 06:05

Nick W


People also ask

How do you call an explicit constructor?

Explicit use of the this() or super() keywords allows you to call a non-default constructor. To call a non-args default constructor or an overloaded constructor from within the same class, use the this() keyword. To call a non-default superclass constructor from a subclass, use the super() keyword.

What is constructor invocation?

The invocation of a constructor constructs a new object of the class to which the constructor belong and returns a reference to the created object. The object is constructed by making use of the parameters passed to the constructor. Example: new String("test") new is the predefined operator.

Why is constructor bad?

Constructors are not evil. They exist so that code can be run when an instance of a class is initialized. Just as with any other programming concept, if they aren't used right they can be a disaster to work with. But, if used correctly, they can be a great (and essential) tool.

What happens if I don't use this keyword in Java?

Definition and Usage The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".


1 Answers

In general, using this to chain constructors is not bad style. It could be bad style in particular examples, but I would only be prepared to make that judgment based on the specific code. Artificial examples (e.g. the semantic-free example in your question) cannot be judged.

It may be that your lecturer has actually "pinged" you on a different issue; e.g.

  • creating a bucket-load of unnecessary / confusing / semantically muddled constructor overloads, or
  • not writing decent javadocs for the constructor overloads.

Both of these would (IMO) be bad style to the degree that they make your code harder to read and maintain.


It is arguable that when you chain constructors that you need to look at more constructors to understand what is going on. But the counter argument is that making all of the constructors (artificially) independent violates the DRY principle.

like image 153
Stephen C Avatar answered Oct 19 '22 20:10

Stephen C