- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reflection Array Class in Java
The array class present in java.lang.reflect package belong to the Java reflection class. The Java Reflection class provides static methods, and these methods can be used to create and access Java arrays in a dynamic manner. This class is final, and this means it can’t be changed or even instantiated. The methods present inside this class can be used with the help of the class name itself.
The methods present in java.util.Arrays.class can be used to work with arrays, and the java.lang.reflect.Array class contains methods that help in creating and working with Java arrays in a dynamic manner.
The java.lang.reflect.Array class is a child of the java.lang.Object class, which is its parent class. It can be declared as follows −
public final class Array extends Object Array.function_name; …
Below is a simple implementation of creating an array of a specific size using the concept of reflection −
Example
import java.lang.reflect.Array; import java.util.Arrays; public class Demo { public static void main(String[] args) { int array_size = 6; int[] int_array = (int[])Array.newInstance(int.class, array_size); System.out.println(Arrays.toString(int_array)); } }
Output
[0, 0, 0, 0, 0, 0]
A class named Demo contains the main function, where an array size is defined. An integer array is defined by specifying the class of the array, and the size of the array. This array is printed on the screen. The output is an array of 5 zeroes, since the size was 5, and no element was specified, thereby taking the default value of 0.
- Related Articles
- List methods of a class using Java Reflection
- Initialize an Array with Reflection Utilities in Java
- Create array with Array.newInstance with Java Reflection
- How to instantiate a static inner class with reflection in Java?
- Using reflection to check array type and length in Java
- Reflect Array class in Java
- Use reflection to create, fill, and display an array in Java
- Create new instance of an Array with Java Reflection Method
- Create new instance of a Two-Dimensional array with Java Reflection Method
- How to prevent Reflection to break a Singleton Class Pattern?
- Call methods of an object using reflection in Java
- Array class in C++
- How to show reflection frames of StackFrame in Java 9?
- What is the class "class" in Java?
- Reflection in C#
