• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing static method reference as a call parameter

 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't Java provide it? I can't find, it looks like I can pass only objects, but static methods do not belong any objects besides scope. Do you think passing Method which can be invoked a good solution? Or maybe just pass instance of Class object or its name and get Method in a called method? Any thoughts?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you trying to accomplish?

(It sounds like you might be looking for a design pattern of some sort, but I can't tell.)
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One can pass methods (static or instance) around via the Method class from the Reflection API. However, if you are indeed a beginner, then this is fairly tricky stuff. It is quite likely that there is a simpler, more object-oriented way of achieving your real goal. Please tell us more.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds to me like you should take a look at the Strategy design pattern.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Sounds to me like you should take a look at the Strategy design pattern.
Could you elaborate?


fyi
Strategy Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.


Spasibo!
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
What exactly are you trying to accomplish?

(It sounds like you might be looking for a design pattern of some sort, but I can't tell.)


Let's say I have a bunch of Java applications which provides a static method cancel(). So when I launch such application I want to pass it in some control module which will be able to cancel its execution. I could wrap cancel method in an object and pass a reference to this object (I think Ilia named it Strategy) however I'd like to pass static method reference directly.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, as has been said, you could use java.lang.reflect.Method objects; for that matter, you could pass the class name and use reflection to find and call the methods. But why are things designed this way in the first place, using static methods? Why not just do it the easy and right way and use a "Cancelable" interface with a cancel() method?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic