0

I came across one method which i couldn't understand.


1. Line number 5, a method add() is called through object prx and then it continues to define this method. Which concept is this?

2. Line 7. Here as method is overidden within method definition. As per my knowledge method can not be defined within method but in this example method addResp() (#7) is defined within prx.add() (#5) and this is defined in IntCall(). (#1) Then how it is possible here?

What concept should i study to understand above code?

1      public void IntCall() throws Exception {
 2         NmbrCalc addres = new NmbrCalc();
 3         for (int i = 0; i < 20; ++i) {
 4            int v1 = i/50;
 5            prx.add(v1, 200, new IListenResponse() {
 6               @Override
 7               public void addResp(int result) {
 8                  addres.setValue(result);
 9               }
10             });
11          }
12       }
user3042916
  • 87
  • 3
  • 12
  • 5
    Anonymous inner classes – J-Alex Apr 18 '18 at 13:22
  • 1
    This is not a method definition but a class impementation of an [anonymous class](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html) – L.Spillner Apr 18 '18 at 13:22
  • Depending upon the definition of `IListenResponse` and your Java version, you may be able to write `prx.add(v1, 200, address::setValue)` instead. – Andy Turner Apr 18 '18 at 13:23

0 Answers0