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 6946297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:32:49+00:00 2026-05-27T13:32:49+00:00

Java Method Class and Java 7’s MethodHandle Class both refer to objects that are

  • 0

Java Method Class and Java 7’s MethodHandle Class both refer to objects that are associated to methods, but still they are rarely used and when a function needs to be passed to another, it is preferred to use an anonymous class that implements an interface that contains one method.

(Note: MethodHandles are supposed to be faster than the old Methods.)

Why aren’t these constructs used more often to pass functions to functions? Is it because they are still verbose?

Code example:

public final class HigherOrder {

public static final List<?> map(final List<?> list, final MethodHandle mh) throws Throwable {
    if (list == null) return null;
    List<Object> ret = new ArrayList<>(list.size());
    for (Object element : list) {
        ret.add(mh.invoke(element));
    }
    return ret;
}

public static final Object reduce(final List<?> list, final MethodHandle mh) throws Throwable {
    if (list == null) return null;
    Object tmp = list.get(0);
    for (int i = 1; i < list.size(); i++) {
        tmp = mh.invoke(tmp, list.get(i));
    }
    return tmp;
}

public static final Integer doubleNumber(final Integer number) {
    return number * 2;
}

public static final Integer sum(final Integer number1, final Integer number2) {
    return number1 + number2;
}

public static void main(String[] args) throws Throwable {
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    MethodHandle doubleNumber = lookup.unreflect(HigherOrder.class.getMethod("doubleNumber", Integer.class));
    MethodHandle sum = lookup.findStatic(HigherOrder.class, "sum", MethodType.methodType(Integer.class, Integer.class, Integer.class));

    List<?> list = Arrays.asList(1, 2, 3, 4, 5);
    System.out.println(list);
    list = map(list, doubleNumber);
    System.out.println(list);
    System.out.println(reduce(list, sum));
}
}

UPDATE

After doing some benchmarking, I noticed that MethodHandle is faster than Reflection’s method, but still nowhere near as fast as a regular method call. Maybe for the method call the JVM can apply some optimizations that are not possible with the handles.

  • 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-05-27T13:32:50+00:00Added an answer on May 27, 2026 at 1:32 pm

    Aside from any performance arguments, passing around Methods or MethodHandles:

    • loses any compile-time safety:
      • number of arguments;
      • types of arguments;
      • the existence of the method itself (you use a string name to find it).
    • loses any declared checked exceptions;
    • makes automated refactoring more difficult.

    Also, as your code demonstrates, using Methods or MethodHandles doesn’t particularly cut down on the amount of code required to declare an object approximating a first-class function:

    // you could even use the Function types from Guava...
    interface Func<In, Out> {
        Out apply(In in);
    }
    
    // create a new "function" object in 5 lines of code:
    Func<String, Integer> parseInt = new Func<String, Integer> {
        public Integer apply(String in) {
            return Integer.parseInt(in);
        }
    }
    

    Admittedly it’s not as nice as a real lambda syntax, but the type-safety, refactoring benefits and ease of explanation make typing out those five lines the least-worst option much of the time.

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

Sidebar

Related Questions

I'm using Java. I want to have a setter method of one class that
Suppose that I have a Java class with a static method, like so: class
Please refer to the Java code below: class Base{ Base(){ System.out.println(Base Constructor); method(); }
In java, the method 'Class.getMethods()' returns an array of all the methods in the
I have a bash script that calls a java class method. The method returns
Is it possible in Java to create a static factory method/class that uses an
During navigation of the java.lang.reflect.Method class I came across the method isBridge . Its
As title says, how do I call a java class method from a jsp,
I am trying to use Java's useDelimiter method on it's Scanner class to do
It is possible in plain Java to override a method of a class programmatically

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.