-2

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.

Boann
  • 48,794
  • 16
  • 117
  • 146
Vivi Xu
  • 131
  • 2
  • 5
  • 12
  • 5
    There's no reflection in your code, and the error is not about reflection. The error comes from the fact that `teacher` is nested inside `student` and is not a static class. Follow a tutorial on the topic and that will make sense. – vanza Sep 11 '14 at 03:52
  • @vanza But after I change teacher to a static class, the result only looks like :student Elodie student$teacher Lady Lee – Vivi Xu Sep 11 '14 at 07:32
  • possible duplicate of [No enclosing instance of type Server is accessible](http://stackoverflow.com/questions/7901941/no-enclosing-instance-of-type-server-is-accessible) – Joe Sep 12 '14 at 10:28
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 23:27

1 Answers1

0

The error you're getting because you're trying to initialize an instance of an inner class.

The inner class can be of 4 types:

  1. Static
  2. Method Local
  3. Anonymous
  4. Other then above these normal inner class

For more info on inner class, see this link.

In your code, you've used normal inner class. To make an instance of it, replace the following:

teacher teacher = new teacher();

with the following:

teacher teacher = student.new teacher();

In order to get your desired result, replace the following:

System.out.println(teacher.getClass().getName() + " " + teacher.getName());

with the following:

System.out.println(teacher.getClass().getSimpleName() + " " + teacher.getName());

As far as reflection is concerned, refer to the comment given by vanza.

Hope this info helps you.

RAS
  • 8,100
  • 16
  • 64
  • 86
  • Hi just FYI, I changed `getName` to `getSimpleName` and it works! But if i replaced `new teacher()` with `student.new teacher` it says "Illegal enclosing instance specification for type student.teacher". But anyway it works, thanks. – Vivi Xu Sep 15 '14 at 10:27