tags:

views:

540

answers:

9

The Java gurunaths (natha नाथ = sanskrit for deity-master-protector) at Sun should condescend to accept the necessity of delegates and draft it into Java spec.

In C#, I can pass a method as a handler referenced as a delegate, without needing to go thro the trouble of creating a class just because I need to pass a method in Java.

What are the reasons that make it unnecessary (besides citing the clunky use of a brand new class for the purpose) or disadvantageous that Sun decided not to have it in Java? What advantages does creating a class or implementing interfaces anonymously have over delegates? I can't think of any, can you?

+4  A: 

Simplicity.

By not introducing the concept of delegates into Java, they made the language simpler. Just like not having properties, indexers, ....

(using a simpler language is not necessarily simpler by the way; probably they should have added delegates but that's not how they made the design decisions)

Mehrdad Afshari
Not having delegates is making my projects complex and bulkier with anonymous classes and extraneous interfaces all over the place.
Blessed Geek
in that case, perhaps your object-oriented design is not quite good?
Bozho
@h2g2java: In my *purely personal* opinion (disclaimer: my favorite language is C#), I agree with you and I think Java is designed so religiously to be "pure". They sacrificed ease of use too much for the purity and simplicity of the language. However, some people (like the designers) definitely disagree. After all, designing a language is hard and you should make a lot of trade-offs. If you add any possible feature to a language, you'll end up reinventing C++ ;).
Mehrdad Afshari
h2g2java: You should take into account that C# was developed 2001 while Java is from 1995, and people learned something in that time.
Aaron Digulla
@Bozho: Perhaps a pure object oriented design is not the best way to solve a problem and a functional paradigm works better. After all, we the ultimate goal of OOP principles is to create better software, not to blindly follow them all.
Mehrdad Afshari
that's quite correct, but _maybe_ using functional techniques looks like "the easy path", while the proper one (the one making the software better in terms of maintainability, readability, stability) lies in the OOP realm; a design pattern perhaps.
Bozho
A: 

Delegates in C# look ugly to me. (declaring something as variable, and then adding () after it feels bad in OOP)

Besides, if you want : "The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

you can simply use java.lang.reflect.Method

Edit: As you said, using reflection is not a good approach. This is true, but using delegates, in an OOP perspective, is not a better approach, in my opinion. It is a functional-language construct.

Bozho
if the downvote is because of the 'offensive' word 'ugly', tell me, I'll change it to "not-beautiful" ;)
Bozho
I did not downvote anyone, but reflection as we all know should be used as a last resort.
Blessed Geek
I agree on that, but it is as OOP-less as delegates are.
Bozho
Many other languages regard functions as object, so declaring a method signature as a type should not be felt as bad OPP. It's like saying you don't like the `::` before the variable names in Fortran when you need multiple attributes.
Cecil Has a Name
java also regards _methods_ as objects, as I noted in my answer. But relying heavily on those is not a good practice.
Bozho
+2  A: 

In Java, you have inner classes, which are tied to one particular instance of the parent class and have direct access to its members. Thus, implementing an inner class does not require (much) more code than writing a method.

C# does not have inner classes. Both inner classes (like in Java) and delegates (like in C#) have their advantages (sometimes I miss inner classes in C#), but from a language designer point of view it makes sense to stick to either one of them (rather than supporting both), since this makes the class library design more consistent.

Heinzi
But you can have nested classes in C#, and they can also access private members of their parent class.
Groo
@Groo: AFAIK, no. The nested class (C#) doesn't automatically get a reference to the parent class. You should pass it explicitly.
modosansreves
Exactly. Nested class != inner class. A nested class in C# corresponds to a "static nested class" in Java; see the link behind "inner classes" in my answer.
Heinzi
+3  A: 

java does not have closures because it was not intended to be a functional language, but an object oriented one.
as in many case you can fake another language paradigm, in this case using anonymous interfaces instead of closures.
but now things are changing and under the pressure of new jvm languages like scala, groovy, jruby, etc., that combines oo and funcional paradigms, the java committee is trying to put closures into java 7.

naaka
I believe closures *will* now go into Java 7, after some committee indecision
Brian Agnew
good!the point is how they will go in!
naaka
+13  A: 

[Minor edits]

Let me first say that I'm not against or in favor of adding delegates to Java. I'm just explaining the background.

First, Sun's Java team has been traditionally more conservative (compared to the C# team) regarding evolution of the language.

Second, Adding a delegate construct into Java would probably require the introduction of a new keyword, such as: "delegate". This will break existing code in which there are variables named "delegate".

Third, there is this design principle called the "Single Choice Principle" http://en.wikipedia.org/wiki/Single%5Fchoice%5Fprinciple. When applied to language design it means that a programmer should have only one obvious way to achieve something. Or, in other words, multiple choices are risky. The introduction of delegates into Java will work against this principle as their behavior can be achieved via anonymous classes.

[Of course, this principle should not be taken literally. If it were then we'd be programming with a good old Turing Machine. I guess the Sun guys felt that delegates do not constitute a benefit that outweighs the single choice violation]

Itay
+1 for the "single choice principle" :)
Bozho
+1 for the "Single Choice Principle". I have admired the simplicity of Java artifacts and it's byte code representation. You have articulated the idea very well.
Chandra Patni
"If it were then we'd be programming with a good old Turing Machine." Actually, Turing machine has never been considered to be a practical hardware model or programming model. Its real purpose is as model for proving certain mathematical results in computation theory.
Stephen C
-1 for not possible to add keyword. You can add context-sensitive keywords without breaking existing code (or just use a fancy operator, as the colon in the foreach loop).
Adrian
I think the Java team is more conservative because they've been in business a lot longer. Backwards compatibility has been a high priority. Perhaps they value it more than the captive audience that Microsoft has.
duffymo
+5  A: 

Here is Tom Ball's account for Microsoft proposal to add them to Java and why Sun rejected them.

IMO, Java should have had closures twelve years back. Gilad Bracha argued for closures and no one listened. In his own words:

I personally argued for adding closures since 1997/98. My blood pressure still rises measurably when I recall the response I got at the time: "Our customers aren't asking for it, so why add it?".

Sad, but true.

Chandra Patni
+1 for the quote. Sadly, true in many other organizations
Itay
"should" in such cases should go with "IMO". I haven't met the need for closures - that said, I don't think it should have had them.
Bozho
@Bozho Your comments are duly noted. We can agree to disagree on having them in Java. In my view, closures and lambda expressions are essential to a language. Slots of an object can either have data (field) or code (function).
Chandra Patni
+1 for quoting Gilad. Closures ftw!
Adrian
-1 Delegates encouarge and allow to much bad software design and this is not recognized enough.
ealgestorm
A: 

Maybe because if a method is to be passed and shared across objects, it shouldn't be owned by one single class. It should probably be shared via its own class. I mean, a transient function does feel a bit odd if you think of it. Bad OO I suppose.

I REALLY like delegates for UI work. It makes window actions so much easier to program.

It may come down to what you think is more negative, a function that has the wrong owner, or (in my case anyway) your code having methods that belong to one class (button clicks, window resize events) not being part of that one class.

Not sure which I prefer.

RandomInsano
A: 

For whatever it's worth, I have implemented callback/delegate support in Java using reflection. Details and working source are available on my website.

Software Monkey
A: 

Because, they thought that:

Bound method references [delegates] are simply unnecessary. [...] Moreover, they detract from the simplicity and unity of the Java language. Bound method references are not the right path for future language evolution.

Taken from this ( old ) white paper:

http://java.sun.com/docs/white/delegates.html

Now Java is considering adding them, once the language have evolved enough. I'm pretty sure they will be in the language soon ( at least sooner than Perl 6 :P )

You can also use: Software Monkey Callback method

OscarRyz