
- 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
How can we create an instance of VarHandle in Java 9?
In general, a Variable Handle is simply typed reference to a variable. It will be an array element, an instance or static field of the class. VarHandle class can provide write and read access to variables under specific conditions. These are immutable and have no visible condition. In addition, they can't be sub-classified, and each VarHandle has a generic type T, which is the type of each variable represented by this VarHandle. The objective of VarHandle is to define a standard for calling equivalents of java.util.concurrent.atomic and sun.misc.Unsafe operations on fields and array elements.
In the below example, we can use MethodHandle.lookup() method to create a VarHandle instance.
Example
import java.lang.invoke.VarHandle; import java.lang.invoke.MethodHandles; public class VarHandleInstanceTest { public static void main(String args[]) { try { VarHandle fieldHandle = MethodHandles.lookup().in(Student.class).findVarHandle(Student.class, "studentId", int.class); System.out.println("VarHandle instance created successfully!!!"); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } } // Stundent class class Student { protected int studentId; private String[] marks; public Student() { studentId = 0 ; marks = new String[] {"75" , "85" , "95"} ; } }
Output
VarHandle instance created successfully!!!
- Related Articles
- How to create static VarHandle in Java 9?
- How can we create an unmodifiable Set in Java 9?
- How can we create an unmodifiable List in Java 9?
- How can we create an unmodifiable Map in Java 9?\n
- How to create JShell instance programmatically in Java 9?
- How can we create a Service Provider interface in Java 9?
- How can we modify an existing module in Java 9?
- How can we get an ID of the running process in Java 9?
- Can we create an object of an abstract class in Java?
- Can we create an object for an interface in java?
- How can we create a multi-release jar(mrjar) using jar tool in Java 9?
- Can we use private methods in an interface in Java 9?
- How can we customize the start of JShell in Java 9?
- How can we implement methods of Stream API in Java 9?
- Create new instance of an Array with Java Reflection Method

Advertisements