Home  >  Core Java

Example of Array in Reflection API in Java

By Arvind Rai, January 16, 2013
Array in reflection API creates array at runtime. Array of int, float, string etc can be created with the required size. Value can be set to array and it can be iterated over the value. newInstance method takes the parameter of class type for array and size of array.
The use of this class is for dynamic creation of array that can be created as normal array throughout the program.
package com.concretepage.reflection;

import java.lang.reflect.Array;

public class ReflectionArrayTest {
	public static void main(String... args){
		String[] strArray= (String[])Array.newInstance(String.class,5);
		Array.set(strArray,0, "A");
		Array.set(strArray,1, "B");
		Array.set(strArray,2, "C");
		
		System.out.println(Array.get(strArray, 0));
		System.out.println(Array.get(strArray, 1));
		System.out.println(Array.get(strArray, 2));
	}

}
Output will be.
A
B
C

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us