Working with variable handlers [JEP 193]

Variable handlers are typed references to variables and are governed by the java.lang.invoke.VarHandle abstract class. The VarHandle method's signature is polymorphic. This provides for great variability in both method signatures and return types. Here is a code sample demonstrating how a VarHandle might be used:

    . . .     class Example     {      int myInt;      . . .     }    . . .     class Sample     {      static final VarHandle VH_MYINT;      static       {        try         {          VH_MYINT =              MethodHandles.lookup().in(Example.class)            .findVarHandle(Example.class, "myInt", int.class);        }         catch (Exception e)         {          throw new Error(e);        }      }    }    . . . 

As you can see in the preceding code snippet, the VarHandle.lookup() performs the same operation as those that ...

Get Java 9: Building Robust Modular Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.