0

Is there any difference between the call to getName() on line 8 and that on line 9.

If yes, then what is it?

This might be very simple but I did my Google search and the only SO result I got was about using this with a field, and not with a method.

class CallingInstanceMethodWithAndWithoutThis {

    private String getName() {
        return "Zarah";
    }

    private void printGetName() {
        System.out.println(getName());
        System.out.println(this.getName());
    }

    public static void main(String [] args) {
        new CallingInstanceMethodWithAndWithoutThis().printGetName();
    }

}
Community
  • 1
  • 1
Solace
  • 8,612
  • 22
  • 95
  • 183
  • 4
    There is no difference. – SatyaTNV Feb 15 '16 at 12:31
  • 1
    You basically included the duplicate of your question in your question. The answer is the same. – pp_ Feb 15 '16 at 12:33
  • @pp_ I mentioned why it isn't an exact duplicate. That one is about fields. This one is about methods. – Solace Feb 15 '16 at 12:37
  • Sure, but why should it be different for functions? – pp_ Feb 15 '16 at 12:40
  • The linked question is about the `this` keyword and so is yours. – pp_ Feb 15 '16 at 12:43
  • 1
    @pp_ why *shouldn't* it be different for functions? If you are just learning the language, it's not obviously the case. There are things you can do with fields that you can't do with functions, and vice versa. – Andy Turner Feb 15 '16 at 12:45
  • If you know what `this` does, you'll know the answer to your question as well. – pp_ Feb 15 '16 at 12:47
  • pp_ Did you just downvote my question for that reason? Why would it be different for fields and functions? Well, many things work differently for different things. It is a completely valid question. The reason I linked to the other question is to let folks know that I have seen that, and I am still not very sure. – Solace Feb 15 '16 at 12:51

3 Answers3

1

There is no difference, it is just a coding convention to use. Moreover you can ask Eclipse for instance to remove automatically "this" when not needed as a Save Action.

Christophe Moine
  • 816
  • 1
  • 7
  • 7
1

For the compiler there is no difference, but it may cause collisions in some cases. Generally not using this will be OK, java compiler is smart enough to recognize our intentions, but it is strongly recommended to use this keyword anyway (e.g. in setters), IMO it's more clear to understand where the method comes from (in case your class extends other or you import some methods statically).

DamianoPantani
  • 1,168
  • 2
  • 13
  • 23
1

There is no difference between using this and not using it.

To check this, in fact, if we perform disassemble using javap -p -c CallingInstanceMethodWithAndWithoutThis the output is below :

  private void printGetName();
    Code:
       0: getstatic     #19                 // Field java/lang/System.out:Ljava/io/PrintStream;
       3: aload_0
       4: invokespecial #25                 // Method getName:()Ljava/lang/String;
       7: invokevirtual #27                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      10: getstatic     #19                 // Field java/lang/System.out:Ljava/io/PrintStream;
      13: aload_0
      14: invokespecial #25                 // Method getName:()Ljava/lang/String;
      17: invokevirtual #27                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V

We can notice that the output at line 0 and line 10 (where we are calling getName() method) are same and there is no difference.