Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is registration in Java?

What is registration in Java? How do one registers a class to a method or another class?

like image 842
susan Avatar asked Dec 02 '25 04:12

susan


1 Answers

Registering is saving a object reference of one class to another.

For example,

   JButton button = new JButton("Click Me");
   ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
          System.out.println("YOU CLICKED ME");
      }
   }; 

   button.addActionListener(listener); // register me

In the above code, Anonymous subclass of ActionListener object is registered to button object of class JButton. The button instance, in turn, will notify the click event by calling actionPerformed() method of registered instance that it saved.

like image 149
Prince John Wesley Avatar answered Dec 03 '25 18:12

Prince John Wesley