Java: interface with a new keyword, how is this possible?

I read some source code from Java libraries, and here I am confused;

This code is from Document.java in the jaxb library, and ContentVisitor is an interface in the same package, how can we instantiate an interface with a new keyword? is not illegal?

public final class Document { . . private final ContentVisitor visitor = new ContentVisitor() { public void onStartDocument() { throw new IllegalStateException(); } public void onEndDocument() { out.endDocument(); } public void onEndTag() { out.endTag(); inscopeNamespace.popContext(); activeNamespaces = null; } } 
+45
java interface
Feb 06 '12 at 9:10
source share
8 answers

In code, you are not instantiating an interface. Rather, the code defines an anonymous class that implements the interface and creates an instance of this class.

The code is roughly equivalent:

 public final class Document { private final class AnonymousContentVisitor implements ContentVisitor { public void onStartDocument() { throw new IllegalStateException(); } public void onEndDocument() { out.endDocument(); } public void onEndTag() { out.endTag(); inscopeNamespace.popContext(); activeNamespaces = null; } } private final ContentVisitor visitor = new AnonymousContentVisitor(); } 
+45
Feb 06 2018-12-12T00:
source share

It's really. It is called Anonymous Class. Look here

We have already seen examples of syntax for defining and instantiating an anonymous class. We can express this syntax more formally as follows:

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

or

 new interface-name () { class-body } 
+10
Feb 06 2018-12-12T00:
source share

It is called anonymous type / class, which implements this interface. Take a look at the tutorial - Local and Anonymous Inner Classes .

+1
Feb 06 2018-12-12T00:
source share

This declaration actually creates a new anonymous class that implements the ContentVisitor interface, and then its instance for this specified scope is completely correct.

0
Feb 06 2018-12-12T00:
source share

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

0
Feb 06 2018-12-12T00:
source share

Notice where the curly braces open - you declare an internal object (called anonymous class ) that implements ContentVisitor and all the necessary methods in place!

0
Feb 06 2018-12-12T00:
source share

This is an embedded interface implementation. The idea is for the compiler to 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 method.

This is the new Oxygene syntax added to the language, allowing Oxygene programmers to work with these events based on the interface in much the same way as Java programmers.

0
Feb 06 2018-12-12T00:
source share

In fact, you just provided an implementation of this interface in an anonymous way. This is quite common and, of course, possible. Look here for more information.

0
Feb 06 2018-12-12T00:
source share



All Articles