Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

java - How can I resolve "an enclosing instance that contains X.Y is required"?

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.

question from:https://stackoverflow.com/questions/7619505/how-can-i-resolve-an-enclosing-instance-that-contains-x-y-is-required

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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,

X x=new X();
X.Y y=x.new Y();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...