an enclosing instance that contains X.Y.Z is required

蓝咒 提交于 2019-12-08 03:51:22

问题


I'm hoping someone can help me out. I've been researching this problem for hours and nobody else seems to have had the same issue (that I've come across). I must be making some really bonehead mistake.

I'm new to Java and have designed a very simple Java program.

I have an abstract superclass called Student. This Student superclass has 3 extending classes called Graduate, Undergraduate, and PartTime. The Student class has several abstract methods and several non-abstract methods. I've verified that all abstract methods defined in Students have been implemented in all 3 extended classes.

Now I'm trying to do something that should be extremely simple. I'm trying to instantiate one of these extended classes. If I do this:

Student student = new Student();

Netbeans says Student is abstract; can't be instantiated. Ok, fine, I understand why abstract classes cannot be instantiated. Then I try this:

Student.Graduate student = new Graduate();

And Netbeans says an enclosing instance that contains studentmanager.Student.Graduate is required (studentmanager is my package name). I can't figure out what that means. However, I did figure out that I can instantiate Student like this without errors:

Student[] student = new Student[1];

However, if I then try to do the next logical thing:

student[0] = new Graduate();

I get the same an enclosing instance... error.

Bottom line is I'd like to know how I can instantiate Graduate. Can anyone help me out? Any insight would be greatly appreciated!

Thanks.


回答1:


Did you define Graduate, Undergraduate, and PartTime as inner classes of Student? That is, like this:

public class Student {
    /* ... */
    public class Graduate extends Student { /* ... */ }
    public class Undergraduate extends Student { /* ... */ }
    public class PartTime extends Student { /* ... */ }
}

Instances of inner classes have an implicit reference to an instance of the enclosing class, so that it can do things like access the enclosing instance's private fields and methods. When you try to instantiate an instance of one of the subclasses, the compiler says "Hey, hold up, I don't know what to set the implicit reference to!" and produces an error.

There are three possible solutions:

  • You could move the three subclasses out of the Student class into their own top-level classes defined in their own files.
  • You could add the keyword static in front of the declarations of the subclasses. This tells the compiler that you do not want an implicit reference from the inner class to the enclosing class. This will prevent you from doing things like accessing Student's private fields and methods from a subclass, but if you don't need to do that anyway, this is moot.
  • If you do need the subclasses to access private members of Student, you can create an instance of Student and then use this special syntax to create an instance of the inner class.

    Student s = new Student();
    Graduate g = s.new Graduate();

The s.new in this example tells the compiler that the new instance of Graduate should have an implicit reference to s.

Note that in this example this is very unlikely to be what you want. It is often useful to have instances of inner classes that can directly modify their enclosing instance's fields for doing things like updating the state of the enclosing class in response to a callback, but I suspect that is not your intention here.




回答2:


If you can change the class definitions, inner classes must be static in order to support instantiation without an enclosing instance. e.g.

// in file Student.java
public class Student {
    public static class Graduate {}
}

or move Graduate into its own Graduate.java file, will let you do

Student.Graduate grad = new Student.Graduate();

Otherwise change the way you are instantiating these classes as Mike D has suggested.



来源:https://stackoverflow.com/questions/11640710/an-enclosing-instance-that-contains-x-y-z-is-required

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!