
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is Variable Handle in Java 9?
Variable Handle is a variable or reference to a set of variables, including other components of a static field, non-static fields, and outer array elements in the heap data structure. It means that Variable Handle is similar to the existing Method Handle. It can be represented by using java.lang.invoke.VarHandle class. We can use java.lang.invoke.MethodHandles.Lookup static factory method to create Variable Handle objects. It can also be used to access a single element in the array, and byte[] array.
Syntax
public abstract class VarHandle extends Object
Example
import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import java.util.Arrays; public class VarHandleTest { public static void main(String args[]) { VarHandle varHandle = MethodHandles.arrayElementVarHandle(int[].class); int[] array = new int[5]; printArray(array); varHandle.set(array, 2, 5); printArray(array); System.out.println(varHandle.get(array, 2)); } private static void printArray(int[] array) { System.out.println(Arrays.toString(array)); } }
Output
[0, 0, 0, 0, 0] [0, 0, 5, 0, 0] 5
- Related Articles
- How to handle an exception in JShell in Java 9?
- What is variable shadowing in java?
- What is a final variable in java?
- What is JAVA_HOME variable in Java Environment?
- What is instance variable hiding in Java?
- What is EOFException in Java? How do we handle it?
- What is InputMisMatchException in Java how do we handle it?
- What is static blank final variable in Java?
- What is a variable, field, property in Java?
- What is Handle?
- What is Module System in Java 9?
- What is Project Jigsaw in Java 9?
- What is a blank uninitialized final variable in java?
- What is blank or uninitialized final variable in Java?
- What is the JLink tool in Java 9?

Advertisements