Java: Interface with new keyword how is that possible?

前端 未结 9 1493
抹茶落季
抹茶落季 2020-12-04 08:21

I was reading some sourcecode from Java libraries, and I am confused here;

This code is from Document.java in jaxb library, and ContentVisitor is an Interfac

相关标签:
9条回答
  • 2020-12-04 09:19

    It's valid. It's called Anonymous class. See here

    We've already seen examples of the syntax for defining and instantiating an anonymous class. We can express that syntax more formally as:

    new class-name ( [ argument-list ] ) { class-body }
    

    or:

    new interface-name () { class-body }
    
    0 讨论(0)
  • 2020-12-04 09:19

    It is inline interface implementation.Here the idea is to have the compiler generate an anonymous class that implements the interface. Then for each method defined in the interface you can (optionally) provide a method with a suitable signature that will be used as the implementation of the interface's method.

    It is the new Oxygene syntax, added to the language to allow Oxygene programmers to work with these interface-based events in much the same way as Java programmers do.

    0 讨论(0)
  • 2020-12-04 09:20

    There's something called anonymous class in java http://www.java2s.com/Code/Java/Class/Anonymous-class.htm

    0 讨论(0)
提交回复
热议问题