tags:

views:

3667

answers:

7
+12  Q: 

Java Delegates?

Does the Java language have delegate features, similar to how C# has support for delegates?

+3  A: 

Short story: no.

Patrick
+1  A: 

No, but here's an article on how to sort of implement them.

Blorgbeard
+2  A: 

As it says in what Patrick linked you want to use inner classes instead.

SCdF
+11  A: 

Not really, no.

You may be able to achieve the same effect by using reflection to get Method objects you can then invoke, and the other way is to create an interface with a single 'invoke' or 'execute' method, and then instantiate them to call the method your interested in (i.e. using an anonymous inner class).

You might also find this article interesting / useful : A Java Programmer Looks at C# Delegates

Matt Sheppard
+1  A: 

While it is nowhere nearly as clean, but you could implement something like C# delegates using a Java Proxy.

John Meagher
+1  A: 

I have implemented callback/delegate support in Java using reflection. Details and working source are available on my website.

Software Monkey
A: 

Have you read this?

http://www.cutthecrap.biz/software/delegates.html

Michael