Array Reflection

The java.lang.reflect package provides an Array class through whichthe values of an array can be accessed. It is usually more effectiveto cast the array after it has been obtained through reflection, butthere are circumstances where that may not be possible. To access thevalues of an array through reflection, you must first obtain the arrayvariable field by using the getField() method from the variablesowning class. The getField() method returns an object of typejava.lang.reflect.Field through which a reference to the membervariable can be obtained. To access the values of the array, you caneither perform a cast, or use one of the various getter methods in theArray class. The following example demonstrates how to do this:

 import java.lang.reflect.*;public final class ReflectArray {  public int[] intArray;  public ReflectArray() {    intArray = new int[10];    for(int i=0; i < intArray.length; i++)      intArray[i] = i;  }  public static final void main(String[] args) {    ReflectArray reflectArray = new ReflectArray();    Field arrayField;    Object obj;    try {      arrayField = ReflectArray.class.getField("intArray");    } catch(NoSuchFieldException e) {      System.err.println("The intArray field does not exist");      System.exit(1);      return; // This is so compiler can perform proper flow control checking    }    try {      // obj now points to the intArray member of reflectArray      obj = arrayField.get(reflectArray);    } catch(IllegalAccessException e) {      System.err.println("The intArray field is not public");      System.exit(1);      return;    }    if(obj instanceof int[])      System.out.println("obj is an integer array");    // It is more sensible at this point to cast the array, but    // it is possible to access its values through reflection.    int length = Array.getLength(obj);    int value;    // Print the array in reverse order    while(length-- > 0) {      value = Array.getInt(obj, length);      System.out.print(value + " ");    }    System.out.println();  }}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job