Java FAQ: Top Questions
40. What are method handles in Java?
Method handles
, introduced in Java 7 in
java.lang.invoke
, provide a low-level, dynamic way to invoke methods, offering more flexibility than reflection but with better performance.
- MethodHandle: A typed reference to a method, field, or constructor.
-
Lookup:
MethodHandles.Lookup
creates handles. - Advantages: Faster than reflection, supports lambdas, avoids access checks after creation.
- Complexity: More complex API than reflection.
- Use Case: Dynamic languages, bytecode generation, advanced frameworks.