Access method of outer anonymous class from inner anonymous class

  • A+
Category:Languages

I instantiate an anonymous class with a method that instantiates another anonymous class, and from this inner anonymous class I want to call a method belonging to the outer anonymous class. To illustrate it, suppose I have this interface:

interface ReturnsANumber {     int getIt(); } 

And then, somewhere in my code, I do this:

    ReturnsANumber v = new ReturnsANumber() {             int theNumber() {                 return 119;             }              public int getIt() {                  // In a modern version of Java, maybe I could do                 //   var a = this;                 // and then call a.theNumber();                  ReturnsANumber w = new ReturnsANumber() {                         int theNumber() {                             return 1;                         }                          public int getIt() {                             return this.theNumber();                         }                     };                  return w.getIt();             }         };     System.out.println("The number is " + v.getIt()); 

Question: In the innermost method getIt, I want to call theNumber() belonging to the outermost anonymous class. How can I accomplish that without using the Java 10 var feature (as hinted in the code).

In other words, how can I make this code display: The number is 119 (instead of displaying The number is 1)

Motivation: Someone might ask why I want to do this anyway: I am writing some sort of code generator and want to be sure that the code that I am generating is not ambiguous.

 


Since Java 8 the solution is pretty easy. Just store the method reference in a variable.

ReturnsANumber v = new ReturnsANumber() {         int theNumber() {             return 119;         }          public int getIt() {              Supplier<Integer> supplier = this::theNumber;              ReturnsANumber w = new ReturnsANumber() {                 int theNumber() {                     return 1;                 }                  public int getIt() {                     return supplier.get();                 }             };              return w.getIt();         }     }; 

Storing outer object could also do the trick. But for inherited methods only:

interface ReturnsANumber {     int theNumber();     int getIt(); }  public int getIt() {     ReturnsANumber outer = this;      ReturnsANumber w = new ReturnsANumber() {         public int theNumber() {             return 1;         }          public int getIt() {             return outer.getIt();         }      };       return w.getIt();  } 

You can store the method reference or the outer object as a field also.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: