Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Classes and Objects in Java
Next article icon

Reflection Array Class in Java

Last Updated : 06 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Array class in java.lang.reflect package is a part of the Java Reflection. This class provides static methods to create and access Java arrays dynamically. It is a final class, which means it can’t be instantiated or changed. Only the methods of this class can be used by the class name itself.

The java.util.Arrays class contains various methods for manipulating arrays (such as sorting and searching), whereas this java.lang.reflect.Array class provides static methods to create and access Java arrays dynamically. This Array class keeps the array to be type-safe.

Class Hierarchy 

java.lang.Object
↳ java.lang.reflect.Array

Class Declaration 

public final class Array 

Note: The Array class should be correctly represented as public final class Array. It extends Object implicitly but does not explicitly declare extends Object.

Syntax to use Array:

Array.<function name>;

Methods in Reflection Array Class in Java

S. No.MethodDescription
1Object get(Object array, int index)This method returns the value of the indexed component in the specified array object.
2boolean getBoolean(Object array, int index)This method returns the value of the indexed component in the specified array object as a boolean.
3byte getByte(Object array, int index)This method returns the value of the indexed component in the specified array object as a byte.
4char getChar(Object array, int index)This method returns the value of the indexed component in the specified array object as a char.
5double getDouble(Object array, int index)This method returns the value of the indexed component in the specified array object as a double.
6float getFloat(Object array, int index)This method returns the value of the indexed component in the specified array object as a float.
7int getInt(Object array, int index)This method returns the value of the indexed component in the specified array object as an int.
8int getLength(Object array)This method returns the length of the specified array object as an int.
9long getLong(Object array, int index)This method returns the value of the indexed component in the specified array object as a long.
10short getShort(Object array, int index)This method returns the value of the indexed component in the specified array object as a short.
11Object newInstance(Class<E> componentType, int length)This method creates a new array with the specified component type and length.
12Object newInstance(Class<E> componentType, int… dimensions)This method creates a new array with the specified component type and dimensions.
13void set(Object array, int index, Object value)This method sets the value of the indexed component of the specified array object to the specified new value.
14void setBoolean(Object array, int index, boolean z)This method sets the value of the indexed component of the specified array object to the specified boolean value.
15void setByte(Object array, int index, byte b)This method sets the value of the indexed component of the specified array object to the specified byte value.
16void setChar(Object array, int index, char c)This method sets the value of the indexed component of the specified array object to the specified char value.
17void setDouble(Object array, int index, double d)This method sets the value of the indexed component of the specified array object to the specified double value.
18void setFloat(Object array, int index, float f)This method sets the value of the indexed component of the specified array object to the specified float value.
19void setInt(Object array, int index, int i)This method sets the value of the indexed component of the specified array object to the specified int value.
20void setLong(Object array, int index, long l)This method sets the value of the indexed component of the specified array object to the specified long value.
21void setShort(Object array, int index, short s)This method sets the value of the indexed component of the specified array object to the specified short value. 

How to create an Array using java.lang.reflect.Array Class?

Creating an array using reflect.Array Class is different from the usual way. The process to create such an array is as follows: 

  • Get the size of the array to be created
  • To create an array (say of X class), use the newInstance() method of Array class to pass the X class as the type of the array, and the size of the array, as parameters.

Syntax: 

X[] arrayOfXType = (X[]) Array.newInstance(X.class, size);

Where X is to be replaced by the type of the array like int, double, etc.

  • This method returns an Object array of the given size, then cast into the required X[] type.
  • Hence the required array of type X has been created.

Below is an example to create an integer array of size 5, using the Array class:

Example: To create an integer array of size 5: 

Java
// Java code to create an integer array of size 5, // using the Array class:  import java.lang.reflect.Array; import java.util.Arrays;  public class GfG {     public static void main(String[] args)     {          // Get the size of the array         int sizeOfArray = 5;          // Create an integer array         // using reflect.Array class         // This is done using the newInstance() method          int[] intArray = (int[])Array.newInstance(             int.class, sizeOfArray);                // Printing the Array content         System.out.println(Arrays.toString(intArray));     } } 

Output
[0, 0, 0, 0, 0] 

How to add elements in an Array using java.lang.reflect.Array Class?

Like creating an array, adding elements in the array using reflect.Array Class is also different from the usual way. The process to add elements in such an array is as follows: 

  • Get the value of the element to be added.
  • Get the index at which the element is to be added.
  • To add an element in an array (say of X class), use the setX() method of Array class, where X is to be replaced by the type of the array such as setInt(), setDouble(), etc. This method takes the X[] as the first parameter, the index at which the element is added as the second parameter, and the element as the third parameter in the below syntax.

Syntax: 

Array.setX(X[], indexOfInsertion, elementToBeInserted);

Where X is to be replaced by the type of the array like int, double, etc.

  • This method inserts the element at the specified index in this array.
  • Hence the required element has been inserted into the array.

Below is an example to add an element into an integer array, using the Array class:

Example: To add an element into an integer array: 

Java
// Java code to add an element into an integer array, // using the Array class:  import java.lang.reflect.Array; import java.util.Arrays;  public class GfG {     public static void main(String[] args)     {          // Get the size of the array         int sizeOfArray = 3;          // Create an integer array         // using reflect.Array class         // This is done using the newInstance() method         int[] intArray = (int[])Array.newInstance(             int.class, sizeOfArray);          // Add elements to the array         // This is done using the setInt() method         Array.setInt(intArray, 0, 10);         Array.setInt(intArray, 1, 20);         Array.setInt(intArray, 2, 30);          // Printing the Array content         System.out.println(Arrays.toString(intArray));     } } 

Output
[10, 20, 30] 

How to retrieve elements in an Array using java.lang.reflect.Array Class?

Like creating an array, retrieving elements in the array using reflect.Array Class is also different from the usual way. The process to retrieve elements in such an array is as follows: 

  • Get the index at which the element is to be retrieved.
  • To retrieve an element in an array (say of X class), use the getX() method of Array class, where X is to be replaced by the type of the array such as getInt(), getDouble(), etc. This method takes the X[] as the first parameter and the index at which the element is retrieved as the second parameter in the syntax below.

Syntax: 

Array.getX(X[], indexOfRetrieval);

Where X is to be replaced by the type of the array like int, double, etc.

  • This method retrieves the element at the specified index from this array.
  • Hence the required element has been retrieved from the array.

Below is an example to retrieve an element from an integer array using the Array class:

Example: To retrieve an element from an integer array: 

Java
// Java code to retrieve an element from an integer array, // using the Array class:  import java.lang.reflect.Array; import java.util.Arrays;  public class GfG {     public static void main(String[] args)     {          // Get the size of the array         int sizeOfArray = 3;          // Create an integer array         // using reflect.Array class         // This is done using the newInstance() method         int[] intArray = (int[])Array.newInstance(             int.class, sizeOfArray);          // Add elements to the array         // This is done using the setInt() method         Array.setInt(intArray, 0, 10);         Array.setInt(intArray, 1, 20);         Array.setInt(intArray, 2, 30);          // Printing the Array content         System.out.println(Arrays.toString(intArray));          // Retrieve elements from the array         // This is done using the getInt() method         System.out.println("Element at index 0: "                            + Array.getInt(intArray, 0));         System.out.println("Element at index 1: "                            + Array.getInt(intArray, 1));         System.out.println("Element at index 2: "                            + Array.getInt(intArray, 2));     } } 

Output
[10, 20, 30] Element at index 0: 10 Element at index 1: 20 Element at index 2: 30 


Next Article
Classes and Objects in Java

R

RishabhPrabhu
Improve
Article Tags :
  • Java
  • Java-Class and Object
  • java-reflection-array
Practice Tags :
  • Java
  • Java-Class and Object

Similar Reads

  • Reflection in Java
    Reflection is an API that is used to examine or modify the behavior of methods, classes, and interfaces at runtime. The required classes for reflection are provided under java.lang.reflect package which is essential in order to understand reflection. So we are illustrating the package with visual ai
    5 min read
  • Object Class in Java
    Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
    8 min read
  • Classes and Objects in Java
    In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh
    12 min read
  • Array get() Method in Java
    The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax Array.get(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. in
    3 min read
  • util.Arrays vs reflect.Array in Java with Examples
    Array class in java.lang.reflect package is a part of the Java Reflection. This class provides static methods to create and access Java arrays dynamically. It is a final class, which means it can’t be instantiated or changed. Only the methods of this class can be used by the class name itself. On th
    2 min read
  • Static class in Java
    Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class
    3 min read
  • java.lang.reflect.Field Class in Java
    The ability of the software to analyze itself is known as Reflection. This is provided by the java.lang.reflect package and elements in Class .Field serves the same purpose as the whole reflection mechanism, analyze a software component and describe its capabilities dynamically, at run time rather t
    5 min read
  • java.lang.reflect.Proxy Class in Java
    A proxy class is present in java.lang package. A proxy class has certain methods which are used for creating dynamic proxy classes and instances, and all the classes created by those methods act as subclasses for this proxy class. Class declaration: public class Proxy extends Object implements Seria
    4 min read
  • Triplet Class in JavaTuples
    A Triplet is a Tuple from JavaTuples library that deals with 3 elements. Since this Triplet is a generic class, it can hold any type of value in it. Since Triplet is a Tuple, hence it also has all the characteristics of JavaTuples:  They are TypesafeThey are ImmutableThey are IterableThey are Serial
    5 min read
  • Array set() method in Java
    The java.lang.reflect.Array.set() is an inbuilt method in Java and is used to set a specified value to a specified index of a given object array. Syntax Array.set(Object []array, int index, Object value) Parameter : array : This is an array of type Object which is to be updated. index : This is the
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences