1

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());

}
Javier Silva Ortíz
  • 2,624
  • 1
  • 10
  • 19
Liam
  • 558
  • 2
  • 13
  • Possible duplicate of [When should I use "this" in a class?](https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class) – borchvm Oct 17 '19 at 19:04

4 Answers4

2

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?!

GhostCat
  • 133,361
  • 24
  • 165
  • 234
0

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!

Erick Kondela
  • 149
  • 1
  • 5
0

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.

Javier Silva Ortíz
  • 2,624
  • 1
  • 10
  • 19
0

The best practice is to override toString method in your class. It's very basic and I doubted if is this a proper answer, or should be the comment.

So, every class is an Object, and there is String toString() method. THAT method returns a String representation of the every instance of some object.

For example:

If you write toString method in your class definition it will override toString method of object.

@Override
String toString(){
    return (name
           + "\n"
           + surname);
}

After this you will always get attributes name (newline)* surname for every instance of your Object. (if name and surname exists)

Here is the simple class where you can find where you must use this keyword...

public class JoJo {

    private String name;
    private String surname;

    public static void main(String[] args) {

        JoJo jojo = new JoJo();
        jojo.setName("Blah");
        jojo.setSurname("Abracadabra");

        System.out.println(jojo);
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    @Override
    public String toString(){
        return (
                name
                + ("\n")
                + surname
                );
    }

}
zlakad
  • 1,286
  • 1
  • 9
  • 15