Access method of outer anonymous class from inner anonymous class
up vote
21
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.
java methods java-8 anonymous-class method-reference
add a comment |
up vote
21
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.
java methods java-8 anonymous-class method-reference
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
add a comment |
up vote
21
down vote
favorite
up vote
21
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.
java methods java-8 anonymous-class method-reference
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
java methods java-8 anonymous-class method-reference
edited Nov 15 at 23:31
ETO
1,129117
1,129117
asked Nov 8 at 16:21
Rulle
962414
962414
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
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
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
add a comment |
3 Answers
3
active
oldest
votes
up vote
17
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();
}
};
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
– SomeDude
Nov 8 at 17:01
@SomeDude As I mentioned, it will workfor inherited methods only
. InterfaceReturnsANumber
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 writevar outer = this;
, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
– Holger
Nov 19 at 9:02
|
show 3 more comments
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.
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
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 callv.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. IftheNumberProxy()
is a method onv
there would also be a similar method onw
.
– SomeDude
Nov 8 at 18:45
add a comment |
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());
}
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
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();
}
};
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
– SomeDude
Nov 8 at 17:01
@SomeDude As I mentioned, it will workfor inherited methods only
. InterfaceReturnsANumber
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 writevar outer = this;
, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
– Holger
Nov 19 at 9:02
|
show 3 more comments
up vote
17
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();
}
};
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
– SomeDude
Nov 8 at 17:01
@SomeDude As I mentioned, it will workfor inherited methods only
. InterfaceReturnsANumber
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 writevar outer = this;
, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
– Holger
Nov 19 at 9:02
|
show 3 more comments
up vote
17
down vote
accepted
up vote
17
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();
}
};
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();
}
};
edited Nov 19 at 9:49
answered Nov 8 at 16:42
ETO
1,129117
1,129117
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
– SomeDude
Nov 8 at 17:01
@SomeDude As I mentioned, it will workfor inherited methods only
. InterfaceReturnsANumber
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 writevar outer = this;
, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
– Holger
Nov 19 at 9:02
|
show 3 more comments
outer.getIt()
will callgetIt()
it will create a neww
and then call againouter.getIt()
, again creates a neww
- creating endless recursive calls.
– SomeDude
Nov 8 at 17:01
@SomeDude As I mentioned, it will workfor inherited methods only
. InterfaceReturnsANumber
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 writevar outer = this;
, which allows you to access newly declared members of the anonymous outer class through the variable, not only inherited ones.
– Holger
Nov 19 at 9:02
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
Nov 19 at 9:02
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
Nov 19 at 9:02
|
show 3 more comments
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.
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
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 callv.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. IftheNumberProxy()
is a method onv
there would also be a similar method onw
.
– SomeDude
Nov 8 at 18:45
add a comment |
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.
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
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 callv.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. IftheNumberProxy()
is a method onv
there would also be a similar method onw
.
– SomeDude
Nov 8 at 18:45
add a comment |
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.
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 Nov 8 at 16:44
answered Nov 8 at 16:39
ernest_k
18.2k41838
18.2k41838
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
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 callv.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. IftheNumberProxy()
is a method onv
there would also be a similar method onw
.
– SomeDude
Nov 8 at 18:45
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
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 callv.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. IftheNumberProxy()
is a method onv
there would also be a similar method onw
.
– 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
add a comment |
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());
}
}
add a comment |
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());
}
}
add a comment |
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());
}
}
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 Nov 8 at 16:48
Ortwin Angermeier
4,4702130
4,4702130
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
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