#Is a Lambda as class method body/implementation possible in Java?

13 messages · Page 1 of 1 (latest)

wintry remnant
#

Is ist possible to have a Lambda-expression as a method body?

class Example {
    static void MyMethod( int a , int b ) -> System.out.println( "test" );
}

This just gives me an error: error: ';' expected after the parameter-list.

Is there a way to write a method like this (i.e. not having a block with braces)?

hexed sableBOT
#

This post has been reserved for your question.

Hey @wintry remnant! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

chrome helm
#

no, lambdas are for implementing entire classes

#

they're for functional interfaces, interfaces with 1 abstract method

crisp cairn
#

A lambda is a variable that contains code

#

It has the type of a interface

#

With only one method

#

Interface lambda = (args) -> {//code};

#

To run the lambda you have to call its Interfaces method

#

So let's say its run

#

Then you do lambda.run();

#

So no you can not use it for the method body