• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to send method as parameter?  RSS feed

 
Justin Thomas
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to send method as parameter to function?


---
I saw something like ClassLoader somewhere somewhen... any ideas?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Thomas wrote:How to send method as parameter to function?


Literally, as stated, you can't. There are multiple possible approaches that can produce a similar effect though.

However, "sending a method as a parameter" is actually a particular solution you're attempting to use. If you take a step back a moment and describe what you're actually trying to accomplish, to which this seemed like the solution, we can help you find which approach best suits what you're trying to do.
 
Justin Thomas
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to provide the ability to execute a method for someone.

Someone will write a class, that has method. I don't know what that someone will write, but I would like to execute that method of someone's when I am being told to. The method name is unknown from beforehand (otherwise I would write ummm general abstract interface and that's it).

To sum it up:
I don't have the class.
I don't have the method.
But I will one day and it would be nice to execute it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do things like that with reflection.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Thomas wrote:How to send method as parameter to function?

Use Python ... and don't drag Java into your nightmare ;).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Thomas wrote:I want to provide the ability to execute a method for someone.

Someone will write a class, that has method. I don't know what that someone will write, but I would like to execute that method of someone's when I am being told to. The method name is unknown from beforehand (otherwise I would write ummm general abstract interface and that's it).

To sum it up:
I don't have the class.
I don't have the method.
But I will one day and it would be nice to execute it.


So, at runtime you'll be given a String, the method name (and possibly another String, the class name), and you want to execute the method named by that String? Then, yes, if that's all there is to it, then as already suggested, reflection is probably a reasonable solution. If there's more to it than that, then a different approach may be more appropriate.
 
Justin Thomas
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You can do things like that with reflection.


+1
Awesome.
Solved

Superb!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!