wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Passing static method reference as a call parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing static method reference as a call parameter" Watch "Passing static method reference as a call parameter" New topic
Author

Passing static method reference as a call parameter

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

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?


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Sounds to me like you should take a look at the Strategy design pattern.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

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

Joined: Feb 07, 2004
Posts: 472

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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?


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing static method reference as a call parameter