?
Solved

What this mean? embedded function in a constructor

Posted on 2002-03-27
4
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

771 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