Junior — Middle
Can you explain what the concept of reflection in programming means?
sobes.tech AI
Answer from AI
Reflection in programming is the ability of a program to examine and modify its structure and behavior at runtime.
In Java, this means that you can obtain information about classes, methods, fields, create objects, and invoke methods dynamically, even if they are not known at compile time.
Example of using reflection in Java:
Class<?> clazz = Class.forName("java.lang.String");
Method method = clazz.getMethod("substring", int.class, int.class);
String str = "Hello, world!";
String result = (String) method.invoke(str, 7, 12); // "world"
This is useful for frameworks, serialization, testing, and other tasks where working with code dynamically is required.