6

Possible Duplicate:
Java - Creating an array of methods

In java can i store a method in a variable? For example can i have an array of methods? If so how would i do this?

Community
  • 1
  • 1
David
  • 14,087
  • 33
  • 73
  • 104
  • 4
    possiblity duplicate http://stackoverflow.com/q/4280727/668970 – developer May 09 '11 at 06:37
  • @Damodar: May or may not be a duplicate, but it's a very good link to give the OP, kudos. – T.J. Crowder May 09 '11 at 07:03
  • @ T.J. Crowder : but SO doesnot encourage duplicates , please go through the SO FAQ http://stackoverflow.com/faq , to know what kind of question should ask. – developer May 09 '11 at 07:17
  • 4
    I agree with @T.J. IMO, it is NOT CLEAR that this is a duplicate of the question that you found. That's why I am NOT voting to close. – Stephen C May 09 '11 at 08:29
  • yes indeed, Stephen Colbert, very cheeky indeed. – David May 09 '11 at 08:54
  • 1
    @Stephen C : what ever it may be reputation, every one is equal here .I raised to moderatory attention. They will decide it. I dnt have any powers on that. – developer May 09 '11 at 10:57
  • @David - damn ... you've pierced my cunning disguise! – Stephen C May 09 '11 at 11:32
  • [Every should stop worrying and love dupes.](http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/). Generally, you should only close as dupe if it is *an exact dupe*. –  May 09 '11 at 17:08
  • @Will Iam i wrong in raising to moderatory attention? – developer May 10 '11 at 07:45
  • @Damodar: Mods in general like to let the community handle dupes, unless the duplication is an indication that something else is wrong (same user re-asking the same question, two different users asking the exact same question, etc). –  May 10 '11 at 13:08

7 Answers7

5

In Java 6 and below, you'd need to use reflection (see the java.lang.reflect package). Java 7 is meant to have some new features in this regard (specifically method handles (JSR 292) and the new "invoke dynamic" stuff). Java 8 (some way off, then) looks set to have lambda expressions (and yes, that link is to an OpenJDK page, but they say Oracle's on board), which aren't quite the same thing, but are related.

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
2

Yeah you can do that. You have the Method class.

AMDG
  • 1,012
  • 14
  • 29
rkg
  • 5,389
  • 7
  • 35
  • 49
2

I will recommend you to hold the output in a variable rather than calling the function again. Because the variable is going to be held in memory as long as it requires. After that automatic garbage collection will take care of that to free it up from the memory. But if you put method in variable that will eat up the memory for its activation record stack each time it gets called. So its good practice to store output in variable instead of method.

Rupeshit
  • 1,460
  • 1
  • 14
  • 23
  • This is a good idea if the value returned by the method will be the same each time, the method has no side effects, and it's cheap to evaluate and/or will always be needed. However, you're changing the behavior of your code by caching the return value. A method with side effects wants to run a certain number of times, a non-deterministic function may give you a different result each time, and a method that's called when it isn't needed wastes CPU cycles (and causes whatever side effects it's designed to cause, regardless). – cHao May 09 '11 at 14:10
1

It is possible if you use reflection and the Method data type.

Nico Huysamen
  • 10,034
  • 9
  • 57
  • 86
1

You can use reflection to get Method object and then you can call invoke(..) on it

Premraj
  • 7,622
  • 8
  • 44
  • 66
1

Look what, for example, returns Class#getDeclaredMethods().

PeterMmm
  • 23,364
  • 13
  • 69
  • 108
1

Yea reflection is one way. But you can use an interface.

interface I {
    int add (int a, int b);
}

say you have a class

class B implements I {
   int add(int a, int b){
        return a + b;
   }
}

now you can create a function like:

doCalculate(I mehthodInterface) {
    \\some calculations
    \\u can also use any other functions defined in this interface
    methodInterface.add(2, 3);
}

Here you can have array of interfaces that are implementing the methods.

thkala
  • 80,583
  • 22
  • 150
  • 196
Amit Gupta
  • 567
  • 4
  • 14
  • That is an excellent idea, but you are more or less creating a method rather than storing it in a variable. Also, `int add(int a, int b);` is ambiguous. The method always has to be overridden, so this method of "storing" a method is inconsistent. Also, you aren't storing a method at all, you're just defining a method outline. These interfaces exist in java as a workaround (mostly) for multiple inheritance, however I find not much use for it as my design involves abstract classes, which in my opinion make interfaces partially obsolete. *Yes I know this is an old post.* – AMDG Nov 26 '14 at 22:50
  • I found this useful. – user1122069 Mar 05 '16 at 07:42