Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8908535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:12:18+00:00 2026-06-15T03:12:18+00:00

In Java 8, methods can be created as Lambda expressions and can be passed

  • 0

In Java 8, methods can be created as Lambda expressions and can be passed by reference (with a little work under the hood). There are plenty of examples online with lambdas being created and used with methods, but no examples of how to make a method taking a lambda as a parameter. What is the syntax for that?

MyClass.method((a, b) -> a + b);
    

class MyClass{
  //How do I define this method?
  static int method(Lambda l) {
    return l(5, 10);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:12:19+00:00Added an answer on June 15, 2026 at 3:12 am

    Lambdas are purely a call-site construct: the recipient of the lambda does not need to know that a Lambda is involved, instead it accepts an Interface with the appropriate method.

    In other words, you define or use a functional interface (i.e. an interface with a single method) that accepts and returns exactly what you want.

    Since Java 8 there is a set of commonly-used interface types in java.util.function.

    For this specific use case there’s java.util.function.IntBinaryOperator with a single int applyAsInt(int left, int right) method, so you could write your method like this:

    static int method(IntBinaryOperator op){
        return op.applyAsInt(5, 10);
    }
    

    But you can just as well define your own interface and use it like this:

    public interface TwoArgIntOperator {
        public int op(int a, int b);
    }
    
    //elsewhere:
    static int method(TwoArgIntOperator operator) {
        return operator.op(5, 10);
    }
    

    Then call the method with a lambda as parameter:

    public static void main(String[] args) {
        TwoArgIntOperator addTwoInts = (a, b) -> a + b;
        int result = method(addTwoInts);
        System.out.println("Result: " + result);
    }
    

    Using your own interface has the advantage that you can have names that more clearly indicate the intent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When we create interface methods, can't we do something like in java : void
I'm looking for a parser that can extract methods from a java class (static
I need to call some methods in Wininet.dll from a Java program. I can
I successfully created an RMI service and client. I can call methods and so
In Java static methods are created to access it without any object instance. It
Is there a input stream extension so I can call methods like I am
I created a program in Java and I designed it so that methods that
I have created a jni project with java code (native methods) and C code.
I have created a Web Service in Java and I have declared my methods
Can you override method in java without using annotations? Because eclipse doesn't support it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.