Access method of outer anonymous class from inner anonymous class











up vote
18
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).



Clarification: Ideally, the outer anonymous class should not need to know that the inner class wants to call its theNumber method. The idea is to come up with some code that lets the inner class unambiguously call any method on the outer class.



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
    Nov 8 at 16:57















up vote
18
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).



Clarification: Ideally, the outer anonymous class should not need to know that the inner class wants to call its theNumber method. The idea is to come up with some code that lets the inner class unambiguously call any method on the outer class.



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
    Nov 8 at 16:57













up vote
18
down vote

favorite









up vote
18
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).



Clarification: Ideally, the outer anonymous class should not need to know that the inner class wants to call its theNumber method. The idea is to come up with some code that lets the inner class unambiguously call any method on the outer class.



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).



Clarification: Ideally, the outer anonymous class should not need to know that the inner class wants to call its theNumber method. The idea is to come up with some code that lets the inner class unambiguously call any method on the outer class.



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 methods java-8 anonymous-class method-reference






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 23:31









ETO

1,03017




1,03017










asked Nov 8 at 16:21









Rulle

947414




947414












  • 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
    Nov 8 at 16:57


















  • 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
    Nov 8 at 16:57
















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
Nov 8 at 16:57




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
Nov 8 at 16:57












3 Answers
3






active

oldest

votes

















up vote
16
down vote



accepted










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.theNumber();
}
};

return w.getIt();
}


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



Update



@Holger proposed another workaround. You can pass your outer object to a lambda:



ReturnsANumber v = new ReturnsANumber() {
...
@Override
public int getIt() {
ReturnsANumber w = Optional.of(this).map(outer ->
new ReturnsANumber() {
int theNumber() {
return 1;
}
public int getIt() {
return outer.theNumber();
}
}).get();
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
    Nov 8 at 17:01










  • @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
    – ETO
    Nov 8 at 17:07










  • He might have meant to call outer.theNumber() within the inner class' getIt()
    – Nicolás Marzano
    Nov 8 at 18:47










  • Yes, you're right. It was a typo.
    – ETO
    Nov 8 at 19:31










  • Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
    – Holger
    yesterday




















up vote
11
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
    Nov 8 at 16:48












  • @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
    – Delioth
    Nov 8 at 18:18










  • @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
    – SomeDude
    Nov 8 at 18:45


















up vote
3
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















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    16
    down vote



    accepted










    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.theNumber();
    }
    };

    return w.getIt();
    }


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



    Update



    @Holger proposed another workaround. You can pass your outer object to a lambda:



    ReturnsANumber v = new ReturnsANumber() {
    ...
    @Override
    public int getIt() {
    ReturnsANumber w = Optional.of(this).map(outer ->
    new ReturnsANumber() {
    int theNumber() {
    return 1;
    }
    public int getIt() {
    return outer.theNumber();
    }
    }).get();
    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
      Nov 8 at 17:01










    • @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
      – ETO
      Nov 8 at 17:07










    • He might have meant to call outer.theNumber() within the inner class' getIt()
      – Nicolás Marzano
      Nov 8 at 18:47










    • Yes, you're right. It was a typo.
      – ETO
      Nov 8 at 19:31










    • Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
      – Holger
      yesterday

















    up vote
    16
    down vote



    accepted










    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.theNumber();
    }
    };

    return w.getIt();
    }


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



    Update



    @Holger proposed another workaround. You can pass your outer object to a lambda:



    ReturnsANumber v = new ReturnsANumber() {
    ...
    @Override
    public int getIt() {
    ReturnsANumber w = Optional.of(this).map(outer ->
    new ReturnsANumber() {
    int theNumber() {
    return 1;
    }
    public int getIt() {
    return outer.theNumber();
    }
    }).get();
    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
      Nov 8 at 17:01










    • @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
      – ETO
      Nov 8 at 17:07










    • He might have meant to call outer.theNumber() within the inner class' getIt()
      – Nicolás Marzano
      Nov 8 at 18:47










    • Yes, you're right. It was a typo.
      – ETO
      Nov 8 at 19:31










    • Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
      – Holger
      yesterday















    up vote
    16
    down vote



    accepted







    up vote
    16
    down vote



    accepted






    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.theNumber();
    }
    };

    return w.getIt();
    }


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



    Update



    @Holger proposed another workaround. You can pass your outer object to a lambda:



    ReturnsANumber v = new ReturnsANumber() {
    ...
    @Override
    public int getIt() {
    ReturnsANumber w = Optional.of(this).map(outer ->
    new ReturnsANumber() {
    int theNumber() {
    return 1;
    }
    public int getIt() {
    return outer.theNumber();
    }
    }).get();
    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:



    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.theNumber();
    }
    };

    return w.getIt();
    }


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



    Update



    @Holger proposed another workaround. You can pass your outer object to a lambda:



    ReturnsANumber v = new ReturnsANumber() {
    ...
    @Override
    public int getIt() {
    ReturnsANumber w = Optional.of(this).map(outer ->
    new ReturnsANumber() {
    int theNumber() {
    return 1;
    }
    public int getIt() {
    return outer.theNumber();
    }
    }).get();
    return w.getIt();
    }
    };






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered Nov 8 at 16:42









    ETO

    1,03017




    1,03017












    • 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
      Nov 8 at 17:01










    • @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
      – ETO
      Nov 8 at 17:07










    • He might have meant to call outer.theNumber() within the inner class' getIt()
      – Nicolás Marzano
      Nov 8 at 18:47










    • Yes, you're right. It was a typo.
      – ETO
      Nov 8 at 19:31










    • Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
      – Holger
      yesterday




















    • 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
      Nov 8 at 17:01










    • @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
      – ETO
      Nov 8 at 17:07










    • He might have meant to call outer.theNumber() within the inner class' getIt()
      – Nicolás Marzano
      Nov 8 at 18:47










    • Yes, you're right. It was a typo.
      – ETO
      Nov 8 at 19:31










    • Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
      – Holger
      yesterday


















    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
    Nov 8 at 17:01




    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
    Nov 8 at 17:01












    @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
    – ETO
    Nov 8 at 17:07




    @SomeDude As I mentioned, it will work for inherited methods only. Interface ReturnsANumber doesn't have such method.
    – ETO
    Nov 8 at 17:07












    He might have meant to call outer.theNumber() within the inner class' getIt()
    – Nicolás Marzano
    Nov 8 at 18:47




    He might have meant to call outer.theNumber() within the inner class' getIt()
    – Nicolás Marzano
    Nov 8 at 18:47












    Yes, you're right. It was a typo.
    – ETO
    Nov 8 at 19:31




    Yes, you're right. It was a typo.
    – ETO
    Nov 8 at 19:31












    Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
    – Holger
    yesterday






    Starting with Java 10, you can write var outer = this;, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
    – Holger
    yesterday














    up vote
    11
    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
      Nov 8 at 16:48












    • @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
      – Delioth
      Nov 8 at 18:18










    • @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
      – SomeDude
      Nov 8 at 18:45















    up vote
    11
    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
      Nov 8 at 16:48












    • @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
      – Delioth
      Nov 8 at 18:18










    • @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
      – SomeDude
      Nov 8 at 18:45













    up vote
    11
    down vote










    up vote
    11
    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 Nov 8 at 16:44

























    answered Nov 8 at 16:39









    ernest_k

    17.8k41836




    17.8k41836












    • 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
      Nov 8 at 16:48












    • @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
      – Delioth
      Nov 8 at 18:18










    • @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
      – SomeDude
      Nov 8 at 18:45


















    • 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
      Nov 8 at 16:48












    • @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
      – Delioth
      Nov 8 at 18:18










    • @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
      – SomeDude
      Nov 8 at 18:45
















    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
    Nov 8 at 16:48






    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
    Nov 8 at 16:48














    @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
    – Delioth
    Nov 8 at 18:18




    @SomeDude That's not true - theNumberProxy() is a closure - it doesn't have any reference to the inner class, it can only call v.theNumber(). TIO
    – Delioth
    Nov 8 at 18:18












    @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
    – SomeDude
    Nov 8 at 18:45




    @Delioth I know , I think you didn't understand what I said, the question specifically said the code is generated. So when you generate code, any instance of an object would get the same set of methods, fields. If theNumberProxy() is a method on v there would also be a similar method on w.
    – SomeDude
    Nov 8 at 18:45










    up vote
    3
    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
      3
      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
        3
        down vote










        up vote
        3
        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 Nov 8 at 16:48









        Ortwin Angermeier

        4,4312130




        4,4312130






























             

            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















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff