Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Passing a method(reference) to an other method and calling themethod.

Reply
Thread Tools

Passing a method(reference) to an other method and calling themethod.

 
 
Erik
Guest
Posts: n/a
 
      03-28-2008
I have a staight forward question. Is is posible to pass a method as a
parameter and call the passed
method from within the other method? Something like the apply function
in Python where can specify which fuction with what parameters is
called (apply("thisMethod", arg1)).
The reason I ask this is because I want to implement a callback
mechanism. How can this be done or is there an other way to implement
a callback mechanism where it's posible to call an arbitrary method?

Thanks.

Erik
 
Reply With Quote
 
 
 
 
Jan Thomä
Guest
Posts: n/a
 
      03-28-2008
Erik wrote:

> I have a staight forward question. Is is posible to pass a method as a
> parameter and call the passed
> method from within the other method?


Straight forward answer - no, not in this way. In dot.net you have a
delegate language construct, however in Java you can help yourself by
creating an anonymous class

interface Callback {
void callback(Object ... args);
}


apply( new Callback() {
void callback(Object ... args) {
thisMethod(args[0], args[1]... );
}
});


Not very nice, but i hear closures are to come in one of the next Java
versions.... You can of course go on reflection or mark callback methods
with annotations or so but this anonymous class is the "easiest" way to do
it...

Jan

--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de

 
Reply With Quote
 
 
 
 
Andrea Francia
Guest
Posts: n/a
 
      03-28-2008
In Java the callback problem are solved passing an Object that hold the
method.
Many times these object are created by an anonymous inner class.

Example API code
----------------

interface Callback {
void callback();
}

void foo(Callback c) {
// do something
...
// call the callback
c.callback();
// do something else
...
}

Example client code:
--------------------

// define callback method
Callback myCallback = new Callback() {
public void callback() {
call the arbitrary method
}
};

// call the library method passing the callback
foo.callback(myCallback);

In groovy which is Java based and you can use the closures.

Alternatively you can use the reflection api which allow you to call a
method specified my name on a specified Object. I don't know much about
the reflection api, so I can't provide you an example.


Erik wrote:
> I have a staight forward question. Is is posible to pass a method as a
> parameter and call the passed
> method from within the other method? Something like the apply function
> in Python where can specify which fuction with what parameters is
> called (apply("thisMethod", arg1)).
> The reason I ask this is because I want to implement a callback
> mechanism. How can this be done or is there an other way to implement
> a callback mechanism where it's posible to call an arbitrary method?
>
> Thanks.
>
> Erik



--
Andrea Francia
http://www.andreafrancia.it/
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-28-2008
Jan Thomä wrote:
> Erik wrote:
>
>> I have a staight forward question. Is is posible to pass a method as a
>> parameter and call the passed
>> method from within the other method?

>
> Straight forward answer - no, not in this way. In dot.net you have a
> delegate language construct, however in Java you can help yourself by
> creating an anonymous class
>
> interface Callback {
> void callback(Object ... args);
> }
>
>
> apply( new Callback() {
> void callback(Object ... args) {
> thisMethod(args[0], args[1]... );
> }
> });
>
>
> Not very nice, but i hear closures are to come in one of the next Java


The anonymous (or named - no need to enforce anonymity) class is just fine -
closures force you to look around for what the closure does. The subclass
technique delegates the method and shows the logic very nicely.

I suspect that even if (when?) they introduce closures to Java, I will
continue to use subtypes anyway, and that the community at large will discover
that the niftiness of closures during development is outweighed by the
difficulty during production maintenance.


--
Lew
 
Reply With Quote
 
Jan Thomä
Guest
Posts: n/a
 
      03-28-2008
Lew wrote:
> I suspect that even if (when?) they introduce closures to Java, I will
> continue to use subtypes anyway, and that the community at large will
> discover that the niftiness of closures during development is outweighed
> by the difficulty during production maintenance.



Well indeed, i find closures a hard to grasp concept. Microsofts delegates
are much more clear and understandable:

delegate MyDelegate(String foo, int i);



public void apply(MyDelegate delegate) {
delegate("10", 10);
}


and for the caller

void myCallbackFunction(String foo, int i) {
}


apply(myCallbackFunction);

That's quite easy, checked by the compiler and nice to use....

--
__________________________________________________ _______________________
insOMnia - We never sleep...
http://www.insOMnia-hq.de

 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      03-28-2008
Jan Thomä wrote:
> Lew wrote:
>> I suspect that even if (when?) they introduce closures to Java, I will
>> continue to use subtypes anyway, and that the community at large will
>> discover that the niftiness of closures during development is outweighed
>> by the difficulty during production maintenance.

>
>
> Well indeed, i find closures a hard to grasp concept. Microsofts delegates
> are much more clear and understandable:


I'm finding your example hard to understand, initially at least.

>
> delegate MyDelegate(String foo, int i);


Is "delegate" a keyword here?

>
>
>
> public void apply(MyDelegate delegate) {


is "delegate" a parameter name here?

> delegate("10", 10);
> }
>
>
> and for the caller
>
> void myCallbackFunction(String foo, int i) {
> }
>
>
> apply(myCallbackFunction);
>
> That's quite easy, checked by the compiler and nice to use....
>


A couple of thoughts:

Although I thought they were ugly at first, I've rather got used to
anonymous/named inner classes for this sort of task.

I recently saw a video where someone Joshu Bloch commented that adding
delegates would require large changes to the language for very little
gain? Possibly http://www.youtube.com/watch?v=ZOwHiGCzZjo

--
RGB
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      03-28-2008
Jan Thomä wrote:
> Well indeed, i find closures a hard to grasp concept. Microsofts delegates
> are much more clear and understandable:
>
> delegate MyDelegate(String foo, int i);
>
>
>
> public void apply(MyDelegate delegate) {
> delegate("10", 10);
> }
>
>
> and for the caller
>
> void myCallbackFunction(String foo, int i) {
> }
>
> apply(myCallbackFunction);
>
> That's quite easy, checked by the compiler and nice to use....


I agree.

C# delegates is a very good OO equivalent of a C function pointer.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      03-28-2008
RedGrittyBrick wrote:
> Jan Thomä wrote:
> I'm finding your example hard to understand, initially at least.
>> delegate MyDelegate(String foo, int i);

> Is "delegate" a keyword here?
>> public void apply(MyDelegate delegate) {

> is "delegate" a parameter name here?
>> delegate("10", 10);
>> }


delegate is a keyword and the code will not compile as written.

He meant something like:

delegate MyDelegate(String foo, int i);

public void apply(MyDelegate md)
{
md("10", 10);
}

Arne

 
Reply With Quote
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-28-2008
Jan Thomä wrote:
> apply( new Callback() {
> void callback(Object ... args) {
> thisMethod(args[0], args[1]... );
> }
> });
>
>
> Not very nice, but i hear closures are to come in one of the next Java
> versions....


Not nice? It illustrates what happens quite clearly, IMO, and makes life
easier for documentation purposes.

The closures feature is still one in hot debate; the last time I checked
(admittedly several months ago), there were two or three different,
non-compatible varieties of closures. It also appears that a majority of
Java developers are at best indifferent to it or even downright hostile.
Joshua Bloch (I believe it was him) gave a presentation a few months ago
as to why he thought that closures were ill-suited to Java, a position
with which I agree.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-28-2008
On Fri, 28 Mar 2008 02:58:17 -0700 (PDT), Erik <>
wrote, quoted or indirectly quoted someone who said :

>I have a staight forward question. Is is posible to pass a method as a
>parameter and call the passed
>method from within the other method?


see http://mindprod.com/jgloss/callback.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling a custom Ruby C extension method from other C extension Iñaki Baz Castillo Ruby 2 04-20-2011 12:24 PM
Passing a method reference and then calling it Martin Hess Ruby 4 06-26-2009 06:35 PM
Calling a method of the calling object ... why-em-jay Perl Misc 3 09-15-2005 07:53 PM
Calling C# method from VB and passing object by reference robertm ASP .Net 1 08-20-2003 03:42 PM
Problems with JNI: calling a Java method from native method. Jabel D. Morales - VMan of Mana Java 1 08-01-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57