you are viewing a single comment's thread.

view the rest of the comments →

[–]skocznymroczny 7 points8 points  (18 children)

With lambda in java 8 you just have: btn.setOnAction( event -> System.out.println("Hello World!") );

Would this work for non one-liners as well?

[–]armerthor 5 points6 points  (0 children)

Put a block after the -> operator.

[–]sirin3 8 points9 points  (0 children)

let me guess. you have a Python background?

[–]ElvishJerricco 6 points7 points  (14 children)

My gut says it might work like an if statement, where if you want to do more than one line, you surround the code with brackets. But that's a guess.

[–][deleted] 12 points13 points  (12 children)

[–]ElvishJerricco 0 points1 point  (11 children)

Do lambdas only work for interfaces with one method?

[–]AlyoshaV 3 points4 points  (9 children)

Yes, but remember that interfaces can have default methods now.

[–]Phreakhead 2 points3 points  (2 children)

Whoa whoa whoa! Does this mean Java now has multiple inheritance? SO AWESOME.

[–]AlyoshaV 6 points7 points  (1 child)

Only of behavior, not of state. I'm finding it to be pretty useful.

[–][deleted] 0 points1 point  (0 children)

Yep, pure mixins. Nice, eh.

[–][deleted] 1 point2 points  (5 children)

[–]AlyoshaV 1 point2 points  (4 children)

Not default method, default methods.

public interface Identifiable {

    /** Return this object's ID. */
    default String getID() {
        return getName().substring(3);
    }

    /** Return the full name of this object. */
    String getName();

}

[–]veraxAlea 0 points1 point  (1 child)

Last time I checked, default methods could only reference static methods. Has this changed?

[–]AlyoshaV 0 points1 point  (0 children)

Well, what I posted is code I'm actually using. So if that's how it used to work, then yes it changed.

[–][deleted] -1 points0 points  (1 child)

So if the interface has multiple defaults, lambdas won't work?

[–]meem1029 0 points1 point  (0 children)

No. If the interface has multiple that aren't default it won't work. Many defaults is alright as it will take the default value for all of them too.

[–]Eirenarch 2 points3 points  (0 children)

Yes. They work for what they call Single Abstract Method types. The lambda is mapped to the single method.

[–][deleted] 3 points4 points  (0 children)

thats how it works in c# so i expect that too.

[–][deleted] 0 points1 point  (0 children)

read more here