Access method of outer anonymous class from inner anonymous class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite












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.










share|improve this question























  • Many promising answers so far. Most of them seem pretty reasonable given that I didn't give more specifications than this. I will wait a bit more to see what kind of answers come up.
    – Rulle
    4 mins ago














up vote
9
down vote

favorite












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.










share|improve this question























  • Many promising answers so far. Most of them seem pretty reasonable given that I didn't give more specifications than this. I will wait a bit more to see what kind of answers come up.
    – Rulle
    4 mins ago












up vote
9
down vote

favorite









up vote
9
down vote

favorite











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.










share|improve this question















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.







java anonymous-class






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 30 mins ago

























asked 40 mins ago









Rulle

870412




870412











  • Many promising answers so far. Most of them seem pretty reasonable given that I didn't give more specifications than this. I will wait a bit more to see what kind of answers come up.
    – Rulle
    4 mins ago
















  • Many promising answers so far. Most of them seem pretty reasonable given that I didn't give more specifications than this. I will wait a bit more to see what kind of answers come up.
    – Rulle
    4 mins ago















Many promising answers so far. Most of them seem pretty reasonable given that I didn't give more specifications than this. I will wait a bit more to see what kind of answers come up.
– Rulle
4 mins ago




Many promising answers so far. Most of them seem pretty reasonable given that I didn't give more specifications than this. I will wait a bit more to see what kind of answers come up.
– Rulle
4 mins ago












3 Answers
3






active

oldest

votes

















up vote
7
down vote













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:



public int getIt() 
ReturnsANumber outer = this;

ReturnsANumber w = new ReturnsANumber()
int theNumber()
return 1;


public int getIt()
return outer.getIt();

;

return w.getIt();






share|improve this answer






















  • outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
    – SomeDude
    1 min ago

















up vote
5
down vote













There is no keyword for accessing the enclosing anonymous class.



But one solution could be proxying the method in the outer anonymous class and making an unqualified reference:



ReturnsANumber v = new ReturnsANumber() 


int theNumber()
return 119;


//Just a different name for theNumber()
int theNumberProxy()
return theNumber();


public int getIt()

ReturnsANumber w = new ReturnsANumber()
int theNumber()
return 1;


public int getIt()
return theNumberProxy(); //calls enclosing class's method

;

return w.getIt();

;


The need for such manoeuvre should be proof enough that your class structure is not ideal and could be a maintenance trap. You could simply replace the first anonymous class with a nested static class, for example.






share|improve this answer






















  • Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
    – SomeDude
    14 mins ago


















up vote
0
down vote













If you can extend the interface:



public class Test 

interface ReturnsANumber
int theNumber();
int getIt();


public static void main(String args)
ReturnsANumber v = new ReturnsANumber()

public int theNumber()
return 119;


public int getIt()

final ReturnsANumber that = this;

// In a modern version of Java, maybe I could do
// var a = this;
// and then call a.theNumber();

ReturnsANumber w = new ReturnsANumber()
public int theNumber()
return 1;


public int getIt()
return that.theNumber();

;

return w.getIt();

;

System.out.println("The number is " + v.getIt());







share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211917%2faccess-method-of-outer-anonymous-class-from-inner-anonymous-class%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    7
    down vote













    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:



    public int getIt() 
    ReturnsANumber outer = this;

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return outer.getIt();

    ;

    return w.getIt();






    share|improve this answer






















    • outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
      – SomeDude
      1 min ago














    up vote
    7
    down vote













    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:



    public int getIt() 
    ReturnsANumber outer = this;

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return outer.getIt();

    ;

    return w.getIt();






    share|improve this answer






















    • outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
      – SomeDude
      1 min ago












    up vote
    7
    down vote










    up vote
    7
    down vote









    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:



    public int getIt() 
    ReturnsANumber outer = this;

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return outer.getIt();

    ;

    return w.getIt();






    share|improve this answer














    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:



    public int getIt() 
    ReturnsANumber outer = this;

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return outer.getIt();

    ;

    return w.getIt();







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 5 mins ago

























    answered 20 mins ago









    ETO

    4607




    4607











    • outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
      – SomeDude
      1 min ago
















    • outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
      – SomeDude
      1 min ago















    outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
    – SomeDude
    1 min ago




    outer.getIt() will call getIt() it will create a new w and then call again outer.getIt() , again creates a new w - creating endless recursive calls.
    – SomeDude
    1 min ago












    up vote
    5
    down vote













    There is no keyword for accessing the enclosing anonymous class.



    But one solution could be proxying the method in the outer anonymous class and making an unqualified reference:



    ReturnsANumber v = new ReturnsANumber() 


    int theNumber()
    return 119;


    //Just a different name for theNumber()
    int theNumberProxy()
    return theNumber();


    public int getIt()

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return theNumberProxy(); //calls enclosing class's method

    ;

    return w.getIt();

    ;


    The need for such manoeuvre should be proof enough that your class structure is not ideal and could be a maintenance trap. You could simply replace the first anonymous class with a nested static class, for example.






    share|improve this answer






















    • Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
      – SomeDude
      14 mins ago















    up vote
    5
    down vote













    There is no keyword for accessing the enclosing anonymous class.



    But one solution could be proxying the method in the outer anonymous class and making an unqualified reference:



    ReturnsANumber v = new ReturnsANumber() 


    int theNumber()
    return 119;


    //Just a different name for theNumber()
    int theNumberProxy()
    return theNumber();


    public int getIt()

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return theNumberProxy(); //calls enclosing class's method

    ;

    return w.getIt();

    ;


    The need for such manoeuvre should be proof enough that your class structure is not ideal and could be a maintenance trap. You could simply replace the first anonymous class with a nested static class, for example.






    share|improve this answer






















    • Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
      – SomeDude
      14 mins ago













    up vote
    5
    down vote










    up vote
    5
    down vote









    There is no keyword for accessing the enclosing anonymous class.



    But one solution could be proxying the method in the outer anonymous class and making an unqualified reference:



    ReturnsANumber v = new ReturnsANumber() 


    int theNumber()
    return 119;


    //Just a different name for theNumber()
    int theNumberProxy()
    return theNumber();


    public int getIt()

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return theNumberProxy(); //calls enclosing class's method

    ;

    return w.getIt();

    ;


    The need for such manoeuvre should be proof enough that your class structure is not ideal and could be a maintenance trap. You could simply replace the first anonymous class with a nested static class, for example.






    share|improve this answer














    There is no keyword for accessing the enclosing anonymous class.



    But one solution could be proxying the method in the outer anonymous class and making an unqualified reference:



    ReturnsANumber v = new ReturnsANumber() 


    int theNumber()
    return 119;


    //Just a different name for theNumber()
    int theNumberProxy()
    return theNumber();


    public int getIt()

    ReturnsANumber w = new ReturnsANumber()
    int theNumber()
    return 1;


    public int getIt()
    return theNumberProxy(); //calls enclosing class's method

    ;

    return w.getIt();

    ;


    The need for such manoeuvre should be proof enough that your class structure is not ideal and could be a maintenance trap. You could simply replace the first anonymous class with a nested static class, for example.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 18 mins ago

























    answered 23 mins ago









    ernest_k

    17.3k41835




    17.3k41835











    • Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
      – SomeDude
      14 mins ago

















    • Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
      – SomeDude
      14 mins ago
















    Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
    – SomeDude
    14 mins ago





    Yes his class design is not ideal and I wonder why would generate code like that. But lets assume one could generate code like that, I am suspecting theNumberProxy() would also be available in the class w . Then it won't work. It would return 1.
    – SomeDude
    14 mins ago











    up vote
    0
    down vote













    If you can extend the interface:



    public class Test 

    interface ReturnsANumber
    int theNumber();
    int getIt();


    public static void main(String args)
    ReturnsANumber v = new ReturnsANumber()

    public int theNumber()
    return 119;


    public int getIt()

    final ReturnsANumber that = this;

    // In a modern version of Java, maybe I could do
    // var a = this;
    // and then call a.theNumber();

    ReturnsANumber w = new ReturnsANumber()
    public int theNumber()
    return 1;


    public int getIt()
    return that.theNumber();

    ;

    return w.getIt();

    ;

    System.out.println("The number is " + v.getIt());







    share|improve this answer
























      up vote
      0
      down vote













      If you can extend the interface:



      public class Test 

      interface ReturnsANumber
      int theNumber();
      int getIt();


      public static void main(String args)
      ReturnsANumber v = new ReturnsANumber()

      public int theNumber()
      return 119;


      public int getIt()

      final ReturnsANumber that = this;

      // In a modern version of Java, maybe I could do
      // var a = this;
      // and then call a.theNumber();

      ReturnsANumber w = new ReturnsANumber()
      public int theNumber()
      return 1;


      public int getIt()
      return that.theNumber();

      ;

      return w.getIt();

      ;

      System.out.println("The number is " + v.getIt());







      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        If you can extend the interface:



        public class Test 

        interface ReturnsANumber
        int theNumber();
        int getIt();


        public static void main(String args)
        ReturnsANumber v = new ReturnsANumber()

        public int theNumber()
        return 119;


        public int getIt()

        final ReturnsANumber that = this;

        // In a modern version of Java, maybe I could do
        // var a = this;
        // and then call a.theNumber();

        ReturnsANumber w = new ReturnsANumber()
        public int theNumber()
        return 1;


        public int getIt()
        return that.theNumber();

        ;

        return w.getIt();

        ;

        System.out.println("The number is " + v.getIt());







        share|improve this answer












        If you can extend the interface:



        public class Test 

        interface ReturnsANumber
        int theNumber();
        int getIt();


        public static void main(String args)
        ReturnsANumber v = new ReturnsANumber()

        public int theNumber()
        return 119;


        public int getIt()

        final ReturnsANumber that = this;

        // In a modern version of Java, maybe I could do
        // var a = this;
        // and then call a.theNumber();

        ReturnsANumber w = new ReturnsANumber()
        public int theNumber()
        return 1;


        public int getIt()
        return that.theNumber();

        ;

        return w.getIt();

        ;

        System.out.println("The number is " + v.getIt());








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 13 mins ago









        Ortwin Angermeier

        4,3812130




        4,3812130



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53211917%2faccess-method-of-outer-anonymous-class-from-inner-anonymous-class%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery