Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid synthetic access compiler warning with inline-anonymous class declaration and what does it mean?

I have the following code:

public class SomeClass {
   //InterfaceUpdateListener is an interface
   private InterfaceUpdateListener listener = new InterfaceUpdateListener(){
        public void onUpdate() {
           SomeClass.this.someMethod();  //complier complains on this line of code
        }
   };

   private void someMethod() {
     //do something in here on an update event occuring
   }

   //other code to register the listener with another class...
}

My compiler in Eclipse complains that

Access to enclosing method 'someMethod' from type SomeClass is emulated by a synthetic accessor method.

Can anyone explain exactly

  1. what this means,
  2. what the possible ramifications might mean if I leave it as is (since its only a warning), and
  3. how I might fix it?

Thanks

like image 445
Chris Knight Avatar asked Aug 12 '11 21:08

Chris Knight


1 Answers

I would just deactivate the rule (i.e. make the compiler not generate warning for this). If the construction is legal, and if an additional method is added by the compiler to support it, then it's the way it must be done.

I doubt there is a significant loss of performance caused by this synthetic method. The JIT must inline it anyway if necessary.

like image 70
JB Nizet Avatar answered Oct 19 '22 23:10

JB Nizet