Why does the new ActionListener instance receive a parenthesis and a; (semicolon) at the end?

5

I've never seen this kind of code structure in the instance, what does it do?    The ActionListener class to instantiate it needs to be put like this:

ActionListener trataEventos = new ActionListener(){
     public void actionPerformed(java.awt.event.ActionEvent e){
     conteudo += e.getActionCommand();
     campoDoFormularioHTML.setMember("value", "" + conteudo);
   }
  }; 

It's similar to doing as a method, why do you need to do this? Create the instance this way?

It follows:

   public class Teclado extends Applet{

   public String conteudo = "";
   public String formulario;
   public String campo;
   public JSObject campoDoFormularioHTML; 

   public void init(){
     formulario = getParameter("formulario");
     campo = getParameter("campo"); 
     ActionListener trataEventos = new ActionListener(){
     public void actionPerformed(java.awt.event.ActionEvent e){
     conteudo += e.getActionCommand();
     campoDoFormularioHTML.setMember("value", "" + conteudo);
   }
  }; 
    
asked by anonymous 01.04.2016 / 16:06

2 answers

3

When you keep your code organized, with correct indentation, it's easier to understand:

 ActionListener trataEventos = new ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent e) {
         conteudo += e.getActionCommand();
         campoDoFormularioHTML.setMember("value", "" + conteudo);
     }
 }; 

Note that the last key closes the start of the ActionListener() method. Every code block that is opened needs to be closed.

The semicolon is just closing an ordinary line, as it always does. Note that the code block is part of only one line of code started in ActionListener trataEventos = new ActionListener() { . This is a line that declares a variable and assigns a value to it. In case this value is a code block of a anonymous class (the class will only exist and can not be used as a type in other places). Within this class you have the definition of a method.

In this case it is necessary because you are creating an event. This is a way to implement the default Observer ( another question ). It is something that needs to react to something that happens in the application. This something is a concrete action that must be defined by some code, a method, so it creates a class that contains this method that will be called when the event occurs. Java 8 introduced a new feature that prevents creating a class just for this.

I'm going to write a code here that is not correct but would help understand what happens in that single line line of code:

 class ClasseTemporaria implements ActionListener {
     public void actionPerformed(java.awt.event.ActionEvent e) {
         conteudo += e.getActionCommand();
         campoDoFormularioHTML.setMember("value", "" + conteudo);
     }
 }

 ActionListener trataEventos = new ClasseTemporaria();

I will not go into more detail because this subject is somewhat advanced and by the history of AP I think that more information to explain the resource of the anonymous class and the specific use, in this case, before understanding other more basic things will only disrupt .

    
01.04.2016 / 16:19
0

The name of this is anonymous class.

ActionListener is an interface, so it needs to be implemented through a concrete class. When an interface needs to be implemented as an inner class in an exclusive location, instead of creating a new class, it uses an anonymous class.

This article explains it best: link

    
01.04.2016 / 16:24