Solved

What this mean? embedded function in a constructor

Posted on 2002-03-27
4
301 Views
Last Modified: 2010-03-31
According to the code below:

//==========================================
public class BrowserPanel extends foo {

  private WebBuilder builder;
  private IlrBuilder.Ruleset ruleset = null;
  private IlrBuilder.Rule rule = null;
  private String packetName = null;

  public BrowserPanel(WebBuilder builder) {
    this.builder = builder;
    IlrWJavaAction ruleAction = new IlrWJavaAction()
    {
      public void perform(IlrWPort port, IlrWComponent source, Object [] args) {
        project = (IlrBuilder.Project)args[0];
        IlrBuilder.Rule rule = (IlrBuilder.Rule)args[2];
        getBuilder().setElement(rule);
        getBuilder().updateFrames(port);
      };
    }
    setSelectPacketAction(packetAction);

    ...
}
//======================

After the new IlrWJavaAction(), what mean the declaration of the embedded function perform()?

When this function can be called and what she's doing in the constructor?


Mario
0
Comment
Question by:mhebert
  • 2
4 Comments
 
LVL 2

Expert Comment

by:userquin
ID: 6899643
Hi:

I do not know anything about IlrWJavaAction class or interface but this example may be obvious:

This example illustrates an example about java.awt.event.ActionListener. As you know (or you must know) ActionListener is an interface to listen button actions. There is a method in button class called addActionListener(ActionListener e) that adds to this button a object that listen to actions performed in this object (button). When a button is clicked (for example), the button (itself) generates an event (ActionEvent) and it calls actionPerformed(ActionEvent e) method of all action listeners added to it self via addActionListener method. So you have a VIEW/CONTROLLER MODEL. Sometimes (when listener has only a method) this listeners are added to the view with anonymous classes so you do not have to create a class to this object. In this example you will have code that look like this:

JButton x = new JButton("...);
x.addActionListener(new ActionListerner() {
  public void actionPerformed(ActionEvent e) {
    ....
    ....
  }
});

When you compile this class, compiler creates an anonynous class called Myclass$1.class. In this case, actionPerformed method will be called when someone clicks the button.

Hope this help.

Bye.

J.
0
 

Expert Comment

by:0xDEADBEEF
ID: 6899681
The IlrWJavaAction object ruleAction in fact is an anonymous class derived from IlrWJavaAction. The declaration of the 'void perform' method specifies what to do if ruleAction.perform(bla) is called.

Anonymous classes are used for classes that are only used once in a very special context (such as event handling).
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6900536
> After the new IlrWJavaAction(), what mean the
> declaration of the embedded function perform()?

It's just defining an (anonymous) inner class.

> When this function can be called and what she's doing in
> the constructor?

The function can be called using:

ruleAction.perform(a, b, c);

In the BrowserPanel ctor she defines an inner class which is a subclass of IlrWJavaAction.
It is identical to the following:

public class BrowserPanel extends foo {

 private WebBuilder builder;
 private IlrBuilder.Ruleset ruleset = null;
 private IlrBuilder.Rule rule = null;
 private String packetName = null;

 public BrowserPanel(WebBuilder builder) {
   this.builder = builder;
   IlrWJavaAction ruleAction = new MyIlrWJavaAction()
   setSelectPacketAction(packetAction);

   ...
  }

  class MyIlrWJavaAction extends IlrWJavaAction
  {
   {
     public void perform(IlrWPort port, IlrWComponent source, Object [] args) {
       project = (IlrBuilder.Project)args[0];
       IlrBuilder.Rule rule = (IlrBuilder.Rule)args[2];
       getBuilder().setElement(rule);
       getBuilder().updateFrames(port);
     };
   }
  }
...

0
 
LVL 92

Expert Comment

by:objects
ID: 6903973
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question