简体   繁体   中英

Calling parent method from within a Thread having the same name

I need to call a method having the identical name as one of the thread's methods. Stripped down example:

class A {
  public void getState() {
    System.out.println("Okay"); //expected to get called
  }
  public void exampleCall() {
    new Thread() {
      @Override public void run() {
        getState(); //gives Thread.getState instead
      }
    }.start();
  } 
}

How can I call A.getState() from within run() ?

Since you're inside an anonymous inner class, you need to explicitly specify the enclosing outer class with the (somewhat confusing) syntax:

A.this.getState();

Where A.this effectively means "the enclosing A instance where this inner class is contained".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM