Liam
Liam

Reputation: 568

Is it good practice to include the this keyword in a class method?

For example I have a method which displays the information about the instance of Employee

//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
    System.out.println("Employee Information");
    seperationLine();
    System.out.println("Name: " + getName());
    seperationLine();
    System.out.println("PPS number: " + getPpsNum());
    seperationLine();
    System.out.println("Salary: " + getSalary());

}

Should I use the thiskeyword in the method and other methods which need to use a attribute or is it not necessary

//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
    System.out.println("Employee Information");
    seperationLine();
    System.out.println("Name: " + this.getName());
    seperationLine();
    System.out.println("PPS number: " + this.getPpsNum());
    seperationLine();
    System.out.println("Salary: " + this.getSalary());

}

Upvotes: 1

Views: 562

Answers (3)

Javier Silva Ortíz
Javier Silva Ortíz

Reputation: 2982

In the specific code you posted, it's not necessary to use the this keyword. However, you must use the this keyword if you have two conflicting names in your context and you want to reference the class member, because this returns a reference to the current object. For instance, assume you have

class Employee
{
    int age;
    int _salary;

    public void setAge(int age)
    {
        this.age = age; //You need this.age in order to differentiate it from the int age parameter of the method.
    }

    public void setSalary(int salary)
    {
        _salary = salary; //No need for this._salary, it's redundant since names don't conflict with each other.
    }
}

As for whether it is good practice or not, it really depends on what's considered good practice for you or for your employer, or according to a coding standard guideline you could been abiding by, either personally or at your work.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140427

This is really more of a style question.

In earlier days, editors weren't smart enough to understand: those are fields of the class, so they could/should be highlighted in a different color (compared to local variables).

But nowadays even small editors understand such things. Therefore you don't gain much from using this in places where it is not required to be used. So, I recommend: only write down this when you have to.

Beyond that, the real issue here: don't write such display() methods. The real world practice: @Override the toString() method, and have that return a meaningful representation of your class.

Then whenever you intend to log or display an instance of your class, you call employee.toString() ... and you use the returned string the way you like it.

Meaning: it is a really bad idea to display to the console. What if you want to log stuff into a file later on for example?!

Upvotes: 2

Erick Kondela
Erick Kondela

Reputation: 149

So long as 'this' is an instance of that particular class, it's still a good practice to embrace. Otherwise you have to initially declare an object in order to apply the methods. You can also use new ClassName().method() on the go. All the best!

Upvotes: 0

Related Questions