here is my coding about reflection:
public class student {
private String name;
public student(){
this.name = "Elodie";
}
public String getName(){return name;}
class teacher {
private String name;
public teacher(){
this.name = "Lady Lee";
}
public String getName() {
return name;
}
}
public static class reflect{
public static void main(String[] args)
{student student = new student();
teacher teacher = new teacher();
System.out.println(student.getClass().getName() + " " + student.getName());
System.out.println(teacher.getClass().getName() + " " + teacher.getName());
}
}
}
I got one error message "No enclosing instance of type student is accessible. Must qualify the allocation with an enclosing instance of type student (e.g. x.new A() where x is an instance of student)."
at teacher teacher = new teacher();
Since i want my coding result look like: student Elodie teacher Lady Lee
so i cannot write like student.new teacher() even if it works.
Btw, can anyone explain a bit about reflection based on my coding? I just know we can use reflection to get info of other classes as long as we know the name of the class.