From: blmblm@myrealbox.com Newsgroups: comp.lang.java.programmer Subject: Re: Passing a Method Name to a Method Date: 23 Jun 2011 17:49:43 GMT Organization: None Lines: 54 Message-ID: <96hclnFf52U1@mid.individual.net> References: <81h4075t4gfjglji1n033rb20025ebho68@4ax.com> X-Trace: individual.net 4IhIjC5m3ffDfKJXPuocKwHXCw+iGkgyiU8koi237j/b0747yL X-Orig-Path: not-for-mail Cancel-Lock: sha1:Aa3dHkhbmzr6M5HRyOBSbKx8MpQ= X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!feed.ac-versailles.fr!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5592 In article , markspace <-@.> wrote: > On 6/22/2011 12:46 PM, Gene Wirchenko wrote: > > Dear Jav'ers: > > > > Maybe I will find this after searching more, but so far, no good. > > > > I want to pass a parameter of a method name to another method. > [ snip ] > You can also do similar things with Runnable or Callable, which just > avoids declaring a type. Since declaring a type isn't that big of a > deal, this is usually poor solution unless you are in fact spawning > threads and using the concurrency API directly. > > public class Parser { > final private Runnable parser; > public Parser( Runnable parser ) { > this.parser = parser; > } > public parse() { > parser.run(); > } > } > > You might call this with an anonymous class rather than declaring a yet > another type, however again that might not really be worth it. I'm > drawing a blank here how to pass in arguments to the run method, I don't think you can, can you? (Someone will probably be along shortly to correct me if I'm wrong. Sort of a :-). ) > so I'll > cheat: > > Parser p = new Parser( new Runnable() { > private final Reader in = System.in; > public void run() { > new JavaLanguage.doIt( in ); > } > } ); > p.parse(); // parse stdin I think this is about the best you can do if you insist on (ab)using [*] an interface whose methods don't have the parameters you want. [*] Not that I've ever done anything like that. (Ha.) [ snip ] -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.