0

Is it correct to write a class without using this keyword? I tried the code and then remove this but nothing changed

public class Dog
{
    String name;

    public Dog(String n){this.name = n;}

    public String getName(){return name;}


    public String toString(){
        return("Hi my name is "+ this.getName());
    }

    public static void main(String[] args){
        Dog tuffy = new Dog("tuffy");
        System.out.println(tuffy);
    }
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
weights
  • 59
  • 1
  • 13
  • You don't need to use `this` anywhere in your example, but we could make some changes to your code such that using `this` would be helpful. – Tim Biegeleisen Jul 05 '18 at 16:27
  • Can you? Yes. Should you? In my *opinion*: No. --- Voted to close as "primarily opinion-based question". – Andreas Jul 05 '18 at 16:27
  • yes, it will work here because you are dealing with multiple classes and objects. If it was multiple classes and objects, then this would have mattered like when one class inherits from the other and assigning values. – LearningToCode Jul 05 '18 at 16:27
  • 1
    https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html – JB Nizet Jul 05 '18 at 16:31

3 Answers3

1

If you don't use this keyword before members of the class, it will be added automatically while compiling. So, referring to any member of the class from some function can be done without this keyword if the function you are at doesn't have the argument with the same name as the member. But if it does then you would have to use this to refer to the class member variable instead of parameter.

Although, programmers often use this keyword even if it is not necessary because of clarity and readability of the code.

wdc
  • 2,623
  • 1
  • 28
  • 41
1

This is covered by any tutorial on the basics of writing class code in Java, including the official ones, so it's worth reading up on class syntax and the this keyword from reputable sources.

Within an instance method or a constructor, this is a explicit reference to the current object. If you omit it, any member reference is still checked against the current object first. As such, this works:

class Dog {
  String name;
  public Dog(String n) { name = n; }
}

because name is a member of the current object. Whether you use this.name or name makes no difference here.

However, something like this will not do anything useful:

class Dog {
  String name;
  public Dog(String name){ name = name; }
}

All this does is take the passed in value, and copy that value onto itself, which is then immediately thrown away when the constructor finishes.

In this case, you need to use this to do anything meaningful:

class Dog {
  String name;
  public Dog(String name){ this.name = name; }
}

Now the value of name will explicitly get copied into a variable with the same name that exists as owned by current object.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
0

this keyword is used avoid shadowing done by variable which you declare in your class. For e.g you have used local variable String name. so use this keyword to avoid shadowing.

public Dog(String name) { this.name = name; }

check out for details: What is the meaning of "this" in Java?

Yogendra Mishra
  • 2,399
  • 2
  • 13
  • 20