java - Invoking method of anonymous class -


java 7

first of all, i'm going simplify example avoid posting unnecesary code. specific concrete example little bit complicated, i' try preserve point.

public class test {      public static void main(string[] args){         test t = new test(){ //<---------------------------------------------------------             public void m(){ //                                                         |                 test t = new test(){//                                                  |                     public void m(){//                                                  |                         //here need invoke inclosing class's m() method                      }                     //other actions                 };             }              public void somemethod(){                 //action             }         };     }      public void m(){      } } 

is possible in java? mean, invoke method of anonymous class way?

no it's impossible because there no reference anonymous classes.

this possible way call instance m() method :

new test(){                                                    public void m(){                                                     }   }.m(); 

by definition according oracle documentation here :

anonymous classes enable make code more concise. enable declare , instantiate class @ same time. local classes except not have name. use them if need use local class only once

so if have use 1 of methods of class have create local one.


Comments