Reputation: 51
What's the difference between java.lang.reflect and java.lang.invoke ?
I know both of us can do reflection but I don't know the difference
In my opinion, reflect allow to collect all the method, field etc and invoke can invoke a method without an object
Upvotes: 2
Views: 1388
Reputation: 38910
Reading java documentation links helps you to understand differences clearly.
Provides classes and interfaces for obtaining reflective information about classes and objects.
Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions.
The java.lang.invoke package contains dynamic language support provided directly by the Java core class libraries and virtual machine.
As described in the Java Virtual Machine Specification, certain types in this package have special relations to dynamic language support in the virtual machine:
The class MethodHandle
contains signature polymorphic methods which can be linked regardless of their type descriptor. Normally, method linkage requires exact matching of type descriptors.
The JVM bytecode format supports immediate constants of the classes MethodHandle
and MethodType
.
Upvotes: 1
Reputation: 533442
reflect
is an older library and invoke
works via MethodHandles.
Note: despite the name, a MethodHandle can wrap a constructor or a field.
In Java 9, there will be VarHandles which despite the name is not limits to variables but can invoke code as well.
Upvotes: 0
Reputation: 100003
I recommend the documentation. To quote the first sentence:
The java.lang.invoke package contains dynamic language support provided directly by the Java core class libraries and virtual machine.
java.lang.reflect, on the other hand, is introspection / reflection.
Upvotes: 0