DEV Community

Janardhan Pulivarthi
Janardhan Pulivarthi

Posted on

Day 21 of 100 - Java: Reflection

Reflection allows the executing Java program to examine or introspect itself and manipulate internal properties of the program.

For example,

import java.lang.reflect.*;

class array1 {
    public static void main(String args[]) {
        try {
            Class cls = Class.forName("java.lang.String");
            Object arr = Array.newInstance(cls, 10);
            Array.set(arr, 5, "this is a test");
            String s = (String)Array.get(arr, 5);
            System.out.println(s);
        }
        catch (Throwable e) {
            System.err.println(e);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Practice Java code at https://github.com/j143/java-reflection-examples

https://www.oracle.com/technical-resources/articles/java/javareflection.html


Potential of Java in the coming years:

Cloud Adoption

cloud adoption

Technology adoption

technology adoption

Top comments (0)