Solved

What this mean? embedded function in a constructor

Posted on 2002-03-27
4
281 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now