Access method of outer anonymous class from inner anonymous class
Clash 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.
java anonymous-class
add a comment |Â
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.
java anonymous-class
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
add a comment |Â
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.
java anonymous-class
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
java anonymous-class
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
add a comment |Â
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
add a comment |Â
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();
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
â SomeDude
1 min ago
add a comment |Â
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.
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 suspectingtheNumberProxy()
would also be available in the classw
. Then it won't work. It would return1
.
â SomeDude
14 mins ago
add a comment |Â
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());
add a comment |Â
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();
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
â SomeDude
1 min ago
add a comment |Â
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();
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
â SomeDude
1 min ago
add a comment |Â
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();
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();
edited 5 mins ago
answered 20 mins ago
ETO
4607
4607
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
â SomeDude
1 min ago
add a comment |Â
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- 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
add a comment |Â
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.
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 suspectingtheNumberProxy()
would also be available in the classw
. Then it won't work. It would return1
.
â SomeDude
14 mins ago
add a comment |Â
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.
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 suspectingtheNumberProxy()
would also be available in the classw
. Then it won't work. It would return1
.
â SomeDude
14 mins ago
add a comment |Â
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.
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.
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 suspectingtheNumberProxy()
would also be available in the classw
. Then it won't work. It would return1
.
â SomeDude
14 mins ago
add a comment |Â
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 suspectingtheNumberProxy()
would also be available in the classw
. Then it won't work. It would return1
.
â 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
add a comment |Â
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());
add a comment |Â
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());
add a comment |Â
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());
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());
answered 13 mins ago
Ortwin Angermeier
4,3812130
4,3812130
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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