5

I have silly question about Java (6/7/8) grammar - are those two snippets of method invocation always equivalent?

  1. with this

    this.myMethod(4);
    
  2. without this

    myMethod(4);
    

Note: Of course the problem is about every number, type and combination of arguments

Weaker statement: given program P, can I create program P' only by deleting this. in front of each and every method invocation?

I've taken into account local classes, anonymous classes, inner classes and various inheritance but could have not find any contradiction. So i believe both snippets are actually the same. Unfortunately I can't manage to find any suitable proof (e.g. from the official grammar).

Could you prove me wrong by contradiction or give me some clues for construction of the equivalence proof? Thanks a lot.

EDIT: the equivalence was proven wrong (see comments below) What about the weaker statement?

Seki
  • 11,135
  • 7
  • 46
  • 70
petrbel
  • 2,428
  • 5
  • 29
  • 49
  • 4
    No, it can't be omitted, you are forgetting static imports. – Sanjay T. Sharma Sep 25 '14 at 16:14
  • Ok, so equivalence is out. And what about the weaker statement? Won't be statically imported methods masqueraded by local ones, so when I omit `this.` the same function is called? – petrbel Sep 25 '14 at 16:25
  • Looking at your weaker statement, yes, you can create a program, but will it be valid and compilable? Think of cases wherein method signature is different between static and member methods. – Sanjay T. Sharma Sep 25 '14 at 16:40
  • Could that even happen? Assuming valid input code, of course, by process of deleting `this.` can't be types violated in any possible way, can they? – petrbel Sep 25 '14 at 16:43
  • Yes, that can happen. Unless you can guarantee that the code doesn't have any static methods which have the same name as object methods, there is always a possibility of messing things up. – Sanjay T. Sharma Sep 25 '14 at 16:57
  • but the arguments don't change - I still don't understand. Could you please provide some contradiction (example)? Thanks – petrbel Sep 25 '14 at 17:46

2 Answers2

3

The Java Language Specification states

  • If the form is MethodName - that is, just an Identifier - then:
    • Otherwise, let T be the enclosing type declaration of which the method is a member, and let n be an integer such that T is the n'th lexically enclosing type declaration of the class whose declaration immediately contains the method invocation. The target reference is the n'th lexically enclosing instance of this.

and

When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked (§15.12), or to the object being constructed

and

  • If the form is Primary . [TypeArguments] Identifier involved, then:
    • Otherwise, the Primary expression is evaluated and the result is used as the target reference.

Primary here referring to this.*.

In both cases, the method is going to be resolved to the same method. Given all this information, there's is no compilable program P' that can be created from a compilable program P.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

There's at least one case where they aren't equivalent. For example, this code

void doStuff(){}
void test(){
    Runnable r = new Runnable(){
        @Override
        public void run(){
            doStuff();
        }
    };
    r.run()
}

Is perfectly valid, while this

void doStuff(){}
void test(){
    Runnable r = new Runnable(){
        @Override
        public void run(){
            this.doStuff();
        }
    };
    r.run()
}

isn't.

So if you have a method defined on a class, an anonymous object declared within that class can call it's methods without using this, but if it uses this it results in a compiler error (assuming it doesn't provide an implementation of the method name itself).

resueman
  • 10,572
  • 10
  • 31
  • 45
  • Fair enough. Given your second code, I could delete `this.` and get the first one (my second question aka Weaker statement). Would that always work? – petrbel Sep 25 '14 at 16:38