ChatGPT解决这个技术问题 Extra ChatGPT

Is not an enclosing class Java

I'm trying to make a Tetris game and I'm getting the compiler error

Shape is not an enclosing class

when I try to create an object

public class Test {
    public static void main(String[] args) {
        Shape s = new Shapes.ZShape();
    }
}

I'm using inner classes for each shape. Here's part of my code

public class Shapes {
    class AShape {
    }
    class ZShape {
    }
}

What am I doing wrong ?

new Shape().new ZShape();. The class ZShape needs an enclosing instance to be instantiated.
move inner class to separate file
@Dimmduh comment should be the answer in this case. They should not be inner classes. Moving them would Identify the other issues with the Shape class that exist.
Not to answer the question here but can I suggest to use inheritance here where AShape and ZShape extend the base class Shapes. Nesting classes is not a really good design for this problem.

P
Peter Lawrey

ZShape is not static so it requires an instance of the outer class.

The simplest solution is to make ZShape and any nested class static if you can.

I would also make any fields final or static final that you can as well.


Making ZShape static totally defeats the purpose of what he's trying to do, which is instantiate a copy of ZShape.
@Cardano making it static makes it easier, not harder.
another simple solution is to make the enclosing class instantiate the inner class, i.e. getting ZShape this way: ZShape myShape = new Shape().instantiateZShape();. It implies the ZShape you get does not exist without a Shape, which is the intent here.
@Peter Lawrey How did you realize that all Shape instances have to use the same ZShape? I don't get it from his source.
There are 2 cases if we want static or an instance. Making it Static will not help always.
K
Kartik

Suppose RetailerProfileModel is your Main class and RetailerPaymentModel is an inner class within it. You can create an object of the Inner class outside the class as follows:

RetailerProfileModel.RetailerPaymentModel paymentModel
        = new RetailerProfileModel().new RetailerPaymentModel();

This answer was really helpful, I never knew you could call new twice in a row (and I've done java for 8+ years!)
You can surely call new operator any number of times until you do not want to keep a reference of that object.
If an object of the inner class is create this way, how does it access the members of the outer class?
Within the inner class itself, you can use OuterClass.this.I don't think there's a way to get the instance from outside the code of the inner class though. Of course, you can always introduce your own property: public OuterClass getOuter() { return OuterClass.this; }
While this syntax is pretty convoluted and makes me sad, this should be the accepted answer.
A
Amit Upadhyay

What I would suggest is not converting the non-static class to a static class because in that case, your inner class can't access the non-static members of outer class.

Example :

class Outer
{
    class Inner
    {
        //...
    }
}

So, in such case, you can do something like:

Outer o = new Outer();
Outer.Inner obj = o.new Inner();

What about Outer.Inner obj = (new Outer).new Inner();
@HussainKMRBehestee, no that wouldn't work for sure. However, this would work Outer.Inner obj = new Outer().new Inner();
But Amit, it works for me. I would glad if you could explain why it should not work.
@HussainKMRBehestee, explanation: I can only guess that the grammar in Java says that to instantiate a class we need to call the constructor, and while calling the constructor () is compulsory. However, C, C++ it's not a must. Here is an example which doesn't work. Moreover, I found this post. which is explaining more about grammar in Java and how they are parsed. I would love to see a sample case when this syntax is working for you.
Oh, my bad, it was a typo, Outer.Inner obj = (new Outer()).new Inner(); hope this time it is ok and thanks for noticing that.
s
s1m0nw1

As stated in the docs:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Thanks! Just getting started.
D
David Walschots

Sometimes, we need to create a new instance of an inner class that can't be static because it depends on some global variables of the parent class. In that situation, if you try to create the instance of an inner class that is not static, a not an enclosing class error is thrown.

Taking the example of the question, what if ZShape can't be static because it need global variable of Shape class?

How can you create new instance of ZShape? This is how:

Add a getter in the parent class:

public ZShape getNewZShape() {
    return new ZShape();
}

Access it like this:

Shape ss = new Shape();
ZShape s = ss.getNewZShape();

А
Антон Лялин
Shape shape = new Shape();
Shape.ZShape zshape = shape.new ZShape();

N
Neuron

In case if Parent class is singleton use following way:

Parent.Child childObject = (Parent.getInstance()).new Child();

where getInstance() will return parent class singleton object.


佚名

I have encountered the same problem. I solved by creating an instance for every inner public Class. as for you situation, i suggest you use inheritance other than inner classes.

public class Shape {

    private String shape;

    public ZShape zShpae;
    public SShape sShape;

    public Shape(){
      int[][] coords =  noShapeCoords;
      shape = "NoShape";
      zShape = new ZShape();
      sShape = new SShape();
    }

    class ZShape{
      int[][] coords =  zShapeCoords;
      String shape = "ZShape";
    }

    class SShape{
      int[][] coords = sShapeCoords;
      String shape = "SShape";
    }

 //etc
}

then you can new Shape(); and visit ZShape through shape.zShape;


A wrong solution. Logical error. If the inner class (e.g. ZShape) requires any field to be set, in the outer class's constructor you must get it! public Shape(String field1_innerClass, int field2_innerClass...){zShape = new ZShape(String field1_innerClass, int field2_innerClass...)...}}
Y
Younes

No need to make the nested class as static but it must be public

public class Test {
    public static void main(String[] args) {
        Shape shape = new Shape();
        Shape s = shape.new Shape.ZShape();
    }
}

S
Suragch

One thing I didn't realize at first when reading the accepted answer was that making an inner class static is basically the same thing as moving it to its own separate class.

Thus, when getting the error

xxx is not an enclosing class

You can solve it in either of the following ways:

Add the static keyword to the inner class, or

Move it out to its own separate class.


R
Reishin

To achieve the requirement from the question, we can put classes into interface:

public interface Shapes {
    class AShape{
    }
    class ZShape{
    }
}

and then use as author tried before:

public class Test {
    public static void main(String[] args) {
        Shape s = new Shapes.ZShape();
    }
}

If we looking for the proper "logical" solution, should be used fabric design pattern


m
mauronet

In case that someone has this issue trying to instance a third party component.

In my case was doing a unit test using this component: ChannelSftp.LsEntry

https://i.stack.imgur.com/1IajI.png

The workaround was:

    ChannelSftp channelSftp =  new ChannelSftp();
    Constructor<ChannelSftp.LsEntry> constructor = (Constructor<ChannelSftp.LsEntry>) ChannelSftp.LsEntry.class.getDeclaredConstructors()[0];
    constructor.setAccessible(true);
    ChannelSftp.LsEntry lsEntry = constructor.newInstance(channelSftp, "file1.txt", null);

I found original code here