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

Asked

Viewed 84 times

5

I’ve never seen this kind of code structure in the instance, what does it do? The class ActionListener To instantiate it, you have to put it like this:

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

It is similar to doing as a method, why you need to create the instance in this way?

Is complete:

   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);
   }
  };
  • 1

    Explain better, what parentheses are you talking about?

  • 1

    This is because you are making a call to an anonymous class when you gave a new in Actionlistener.

  • Anonymous class that exists?

  • 2
  • 1

    Anonymous classes, they have their reason to be but in java have always been used most of the time to plug the hole that was the lack of support to Amble before version 8.

  • Okay @Diegof I will study this. D

Show 1 more comment

2 answers

4


When we keep the code organized, with correct indentation becomes easier to understand:

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

I put in the Github for future reference.

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

The semicolon is just closing a common line, as is always done. 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 this case this value is a code block of a anonymous class (class will only exist there and cannot be used as type elsewhere). Within this class you have the definition of a method.

In this case it is necessary because it is creating an event. It is a way of implementing the observer standard (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 resource that avoids creating a class just for this.

I’m going to write a code here that is not correct but it would help to understand what happens in that only code line:

 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 a little advanced and by the history of the AP I think more information to explain the anonymous class resource and the specific use, in this case, before understanding other more basic things will only hinder.

  • Thanks, I get it.

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 internal class in a unique location, instead of creating a new class, use an anonymous class.

This article explains better: http://www.devmedia.com.br/classes-anonimas-e-aninhadas-em-java/31167

  • And the ;(point-and-comma)?

  • 1

    Because this instantiating the object, it is as if it were Actionlistenner treatedEvents = new Classeactionlistenner();

  • : ) okay. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.