如何解决“需要包含 XY 的封闭实例”?
我正在 Netbeans 中开发一个小型桌面应用程序。这是我的第一个程序,我面临着一种非常奇怪的错误。我知道我做错了一些事情,但无法追踪我做错了什么:(
请帮助我解决此错误。
描述: 我有一个默认包 Src
,并且我根据需要在此包中创建新的 Java 类。与其他类一起,我创建了一个类 X
,如下所示:
public class X
{
public class Y
{//some member functions and variables exist here}
public class Z
{//some member functions and variables exist here}
//some member functions and variables exist here
}
现在我需要在同一包中存在的其他类中创建内部类之一的实例,如下所示:
public X.Y oY = new X.Y();
但我得到了以下错误:
需要包含 XY 的封闭实例
请帮助我解决此错误。
I am developing a small desktop application in Netbeans. This is my first program and I am facing a very strange type of error. I know I did some thing wrong but unable to trace what I am doing wrong :(
Please help me in resolving this error.
Description:
I have a default package Src
and I am creating new Java classes in this package as required. Along with other classes I made a class X
like this:
public class X
{
public class Y
{//some member functions and variables exist here}
public class Z
{//some member functions and variables exist here}
//some member functions and variables exist here
}
Now I need to create instance of one of the inner classes in some other class that exists in the same package, like this:
public X.Y oY = new X.Y();
but I am getting the following error:
an enclosing instance that contains X.Y is required
Please help me in resolving this error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您必须创建一个 X 类(外部类)的对象,然后使用 objX.new InnerClass() 语法创建 Y 类的对象。
尝试,
First of all you have to create an object of X class (outer class) and then use
objX.new InnerClass()
syntax to create an object of Y class.Try,
您想要声明静态内部类:
public static class Y
。You want to declare static inner classes:
public static class Y
.将 Y 声明为静态以避免创建 X 的实例。
Declare Y as static to avoid creating instance of X.