-2

I am reading some java code and I came across the following unfamiliar syntax:

controler.addOverridingModule( new AbstractModule() {
        @Override
        public void install() {
            this.addPlanStrategyBinding("RandomTripToCarsharingStrategy").to( RandomTripToCarsharingStrategy.class ) ;
            this.addPlanStrategyBinding("CarsharingSubtourModeChoiceStrategy").to( CarsharingSubtourModeChoiceStrategy.class ) ;
        }
    });

I am confused because the developer created a new method ("install") within the call to addOverridingModule. Could someone please tell me what is going on here?

Thanks!

  • 1
    the method install is a method of the inner class `AbstractModule`. – Jens Jul 04 '16 at 06:26
  • The entire expression consisting of `new AbstractModule { /* ... */ }` is an *anonymous class instantiation*. Off to find a dupe target... – T.J. Crowder Jul 04 '16 at 06:27

1 Answers1

0

addOverridingModule expects an instance of type AbstractModule (which can be a class or an interface - it's not clear which of the two is it in your example, but it doesn't matter).

This snippet is passing to it an instance of an anonymous class that implements/extends AbstractModule. install is a method required by AbstractModule.

Eran
  • 387,369
  • 54
  • 702
  • 768