kaushik
kaushik

Reputation: 2492

using Classname.this.methodname

I saw the following code:

 mActionMode = OverviewActivity.this
        .startActionMode(mActionModeCallback);

I saw this here in the Android Dev. tutorial

What is the benefit of calling function like this ? I have changed the code to:

mActionMode = startActionMode(mActionModeCallback);

but, I didn't see any change.

Upvotes: 0

Views: 328

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533492

The difference (if there is any) is that it calls the outer classes method.

class Outer {
   void methodA() { }

   class Inner {
       void methodA() { }

       void method() {
            methodA(); // call my methodA();
            Outer.this.methodA(); // calls the Outer.methodA();
       }
   }
}

It is possible the developer liked to be specific even if he/sge didn't need to be.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500385

It's useful when you have an outer class with a member with the same name as a nested class member:

public class Test {

    public static void main(String[] args) {
        new Test().new Inner().run();
    }

    class Inner {
        public void run() {
            foo(); // Prints Inner.foo
            Test.this.foo(); // Prints Test.foo
        }

        public void foo() {
            System.out.println("Inner.foo");
        }
    }

    public void foo() {
        System.out.println("Test.foo");
    }
}

Upvotes: 1

Related Questions