Computer Science Canada Methods as parameters |
Author: | Aziz [ Sat Jul 22, 2006 2:43 pm ] | ||
Post subject: | Methods as parameters | ||
Is it possible to specify a method as a parameter in java? For example:
As far as I know, no, but I was seeing if anyone else has add experience with this. |
Author: | wtd [ Sat Jul 22, 2006 3:09 pm ] | ||||
Post subject: | |||||
Not in Java. In many other languages this is possible, but not in Java. However, you can pass an anonymous inner class. My intro Java tutorial talks about these. Consider a very simple:
Or, again in Scala, just for fun.
|
Author: | Aziz [ Mon Jul 24, 2006 4:19 pm ] |
Post subject: | |
Been looking around, and I found the java.lang.reflect package, it's description: Java API Docs wrote: Provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.
AccessibleObject allows supression of access checks if the necessary ReflectPermission is available. Arrays provides static methods to dynamically create and access arrays. Classes in this package, along with java.lang.Class accommodate applications such as debuggers, interpreters, object inspectors, class browsers, and services such as Object Serialization and JavaBeans that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class. And more specificially, the Method class. Description: Java API Docs wrote: public final class Method
extends AccessibleObject implements GenericDeclaration, Member A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method). A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur. Not sure what you could do with it yet, I haven't found much functionality, but I haven't looked to much at the rest of the package. It says often to refer to the Java Language Specification, particularly this section. Check it out anyone who's interested, might be useful? I think wtd or rizzix could find its appropriate use. |
Author: | Aziz [ Tue Jul 25, 2006 9:42 am ] | ||||||||
Post subject: | |||||||||
I was fooling around a little bit, and came up with this. What do you think, wtd?
It works, and could be implemented into program fairly easily. I've tried doing with functions, but just not working the way I like it. Here's what I've got so far:
Not to mention the number or compile errors, I believe I'm absolutely going about this the wrong way. |
Author: | rizzix [ Wed Jul 26, 2006 10:45 am ] | ||||||||
Post subject: | |||||||||
Yes reflection is the way we go about doing such stuff in Java.. Let's take your pseudo-method for instance...
We would rewrite it as follows to make use of reflection:
Let's say we have a class as such:
Then, we may invoke it like this:
The getDeclaredMethod of the class object takes the name of the method and the method signature (an array of classes) as variable arguments. It then retuns the respective method. While getDeclaredMethod returns static methods of a class, getMethod will return instance methods of the class. Both getMethod and getDeclaredMethod throw an Exception if a matching method was not found. So you have to enclose that code withing a try-block. |
Author: | rizzix [ Wed Jul 26, 2006 10:53 am ] |
Post subject: | |
As far as possible DO NOT use reflection. Simply because it is not type safe. |