This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing functions to functions  RSS feed

 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way by which functions can be passed as arguments to functions? If yes, how? If no, what is the philosophy behind not providing such a feature?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call methods, not directly, but using Reflection.
See classes like Class (getMethod()) and Method (invoke()) for more.
 
Daniel Bryant
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In addition to Satou's answer, I have a few thoughts.

You can pass the results of one method (function) into another i.e.


and you can also pass an object into a method allowing you to call any of it's public methods in the corresponding method i.e.



I hope this helps.

Daniel
[ July 24, 2006: Message edited by: Daniel Bryant ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neelesh Bodas:
Is there a way by which functions can be passed as arguments to functions? If yes, how? If no, what is the philosophy behind not providing such a feature?


I am going to guess you come from a C programming background. You need to think more object-oriented, then you won't often need the C "function pointer" idea.

Most of the time, what you would have done in C using function pointers can be done in Java by passing an object that implements an interface. By having different implementations of that interface, you can cause different things to happen, like you would by passing different function pointers.

As another poster has already said, for those rare cases where you really do need to pass a method to a function, you can use reflection. You can pass a Method object, for instance.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to guess you come from a C programming background.

No. From a Functional programming background (Haskell and Scheme) where functions are treated as first class. Thats why the question. I guess I should have clarified.

you can use reflection. You can pass a Method object, for instance.


Yeah, I will have a look at both of these. Thanks all of you for the replies.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neelesh Bodas:
No. From a Functional programming background (Haskell and Scheme) where functions are treated as first class. Thats why the question. I guess I should have clarified.


OK, sorry. And respect for the esoteric languages (blatant trolling - no need to respond .

My point still applies. You are now using a different type of language and it is important to get your brain into thinking in the way that suits that language, rather than trying to simulate another language.

Reflection is relatively slow (though not too bad these days), and very difficult to read and maintain (very important issues in commercial code).

So, when you think you want to use reflection, take a step back and ask yourself, "am I sure that I couldn't do this by an object-oriented approach".
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When anonymous inner classes were first introduced, they were often mentioned as the Java alternative to function pointers. They're not exactly that, but they are a convenient way to say "Here, execute this" and pass a bit of code, along with a useful context for execution, to another object. So, for example, look at the javax.swing.Timer class. You give it an ActionListener, and it calls its actionPerformed() method periodically. ActionListener is an interface with one method, so really, you're passing this one function to the Timer. If you implement that one method in an anonymous inner class -- i.e.,



That's about as close to a lamda expression as you can get in Java.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Peter. Google for the Strategy design pattern - that's what you probably want to use instead of reflection.
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
I agree with Peter. Google for the Strategy design pattern - that's what you probably want to use instead of reflection.


I want to stress this point as well. While reflection is one way to pull this off, generally, the strategy pattern (and similar ideas) is a much better way.

Bascially, instead of passing the method, encapsulate the method in an object, and then pass the object.

- Adam
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!