What type of Java constructor call is this?

I have never encountered anything like this, and I do not know this type of coding! What is it? (I'm new to Java)

DefaultHandler handler = new DefaultHandler() { boolean bfname = false; boolean blname = false; boolean bnname = false; boolean bsalary = false; public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException { // code } public void endElement(String uri, String localName, String qName) throws SAXException { // code } public void characters(char ch[], int start, int length) throws SAXException { // code }; 

After calling the constructor, there is a bracket (!?), And it seems that there is an override of some methods. Then the bracket ends with a semicolon. I never saw the brackets after calling the constructor. This is normal? How does is called? Thanks!

ps: on Eclipse, if I delete the semicolon, it says LocalVariableDeclarationStatement error.

+6
source share
5 answers

This is an anonymous class .

Anonymous classes can be useful if you want to create a class that comes from another class or interface, but you don't need to use your new class anywhere else in your code.

One of the most elegant things about anonymous classes is that they allow you to define a one-shot class exactly where it is needed. In addition, anonymous classes have a concise syntax that reduces clutter in your code.

In your particular case, the DefaultHandler class is a helper class that implements several interfaces ( EntityResolver , DTDHandler , ContentHandler , ErrorHandler ), providing methods that do nothing. The idea is that you can extract from this class and override only those specific methods that you need. This can be much less code than directly implementing the interfaces, because then you will need to provide definitions for each method, including methods that you are not going to use.

+5
source

this is an anonymous class definition. DefaultHandler is an interface and has no implementation, and you create it there, creating an instance.

since DefaultHandler is an interface that expects a class object that implements the DefaultHandler interface. But if there is no such class or you need another, you can create an object that satisfies this requirement by implementing the interface on the go.

+5
source

This is an anonymous inner class. Google has a "anonymous java class". This is basically a class created on the fly from the interface. The whole definition is defined by inline, considering it to be a class definition after the โ€œnew DefaultHandler () functionโ€, it is obvious that this particular class definition can only be used in this place.

+4
source

This is an anonymous class definition. This is basically a way to implement an abstract class or class extension. Thus, you either implement the DefaultHandler class, or extend ing DefaultHandler , depending on whether the DefaultHandler an abstract class or a specific class.

+1
source
0
source

Source: https://habr.com/ru/post/907803/


All Articles