0

In my Java class I have made some attributes and methods. Within one of the methods I have called upon an attribute of the class, however, the method works both with and without using "this." in front of the attribute. If this is not necessary, I am wondering what is even the purpose of "this."

public String getName() {
        return name;
    }

^^ Works ^^

public String getName() {
        return this.name;
    }

^^ Works ^^

surew1n
  • 13
  • 3
  • 2
    Does this answer your question? [When should I use "this" in a class?](https://stackoverflow.com/questions/2411270/when-should-i-use-this-in-a-class) – lakshman Feb 05 '20 at 04:23

5 Answers5

1

From https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html :

"The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter."

The use of "this" is not mandatory. But it is useful to refer to a field of the object instead of a parameter of a method or a variable. See:

public class Person {

  private String name = "John Smith";

  public String getName() {
        return name; // no need for keyword "this"
    }

  public String getFalseName() {
        String name = "Bill Doe"; // this.name is shadowed
        return name; // will return "Bill Doe" since keyword "this" is not used
  }

  public String getTrueName() {
        String name = "Bill Doe"; // this.name is shadowed
        return this.name; // Will return "John Smith";
  }

  public void setName( String name ){ // name is a parameter, this.name is shadowed now
    this.name = name; // this.name is correctly assigned
  }

}
0

Using this is a more direct way of referencing the attributes of the class. You have (correctly) found it is not necessary in this context. Here is a more useful example:

public String getName(String name) {
    return this.name + " - " + name;
}

This will return the attribute name, followed by a hyphen, followed by the name passed into the parameter. By using the this keyword, you can specify exactly which variable you are referring to, even if they have the same name.

jarthur
  • 393
  • 1
  • 2
  • 18
0

Keyword 'this' in Java is a reference variable that refers to the current object. It can be used to refer current class instance variable.

In your case if you write only name then it can refer to current method variable or argument if you write this.name then it will specifically point to current class instance variable.
Mainly this represent as instance of current class.

Dipankar Baghel
  • 1,932
  • 2
  • 12
  • 24
0

Purpose of this: it refers to the very object the method is part of.

Is this pointless? No. How do you otherwise pass the self object as another method's argument? For example, it is common to invoke a function like foo(this)

Bigman
  • 1,363
  • 11
  • 17
0

The main purpose of 'this' keyword is calling the Instance method/variables using object reference of the current class without creating an object.

please follow the below code you will get a little bit clarification about this keyword.

please execute the below code and observe the difference.

public class Test {

private String name;

public String getName() {
    return name;
}

public String getName2() {
    return this.name;
}

public void setName(String name) {
    //here I am asigning method parameter value to class level variable
    this.name = name;
}

public void setName2(String name) {
    //here  Iam not using this keyword 
    //Here There is no use of assign name = name 
    name = name;
}

public static void main(String[] args) {

    Test test = new Test();

    test.setName("test 123");

    System.out.println(test.getName());
    System.out.println(test.getName2());

    test.setName2("test 456");

    System.out.println(test.getName());
    System.out.println(test.getName2());

    test.setName("test 789");

    System.out.println(test.getName());
    System.out.println(test.getName2());
}

}

Ranjith Bokkala
  • 379
  • 1
  • 10