What is Reflection in Java and why is it needed?
Hello everyone! I've been learning Java for about six months, writing simple applications, and I came across the term "reflection" (Reflection API). I read the Oracle documentation but didn't fully understand it.
I have a simple class:
```java
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void sayHello() {
System.out.println("Hello, I'm " + name);
}
private void secretMethod() {
System.out.println("Secret method!");
}
}
```
I understand that usually we create an object via `new User("Ivan", 25)` and call methods directly. But why do we need to get information about a class at runtime? When can this be useful?
What confuses me is that
No answers yet.
Log in to leave an answer
Blogs