Is not an enclosing class Java

JavaInner Classes

Java Problem Overview


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 ?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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();

Solution 3 - Java

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();

Solution 4 - Java

As stated in the docs:

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

Solution 5 - Java

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();

Solution 6 - Java

Shape shape = new Shape();
Shape.ZShape zshape = shape.new ZShape();

Solution 7 - Java

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;

Solution 8 - Java

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();
    }
}

Solution 9 - Java

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.

Solution 10 - Java

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.

Solution 11 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionV SebiView Question on Stackoverflow
Solution 1 - JavaPeter LawreyView Answer on Stackoverflow
Solution 2 - JavaVishal KumarView Answer on Stackoverflow
Solution 3 - JavaAmit UpadhyayView Answer on Stackoverflow
Solution 4 - JavaBrennan MillerView Answer on Stackoverflow
Solution 5 - JavaManoj Bhakar PCMView Answer on Stackoverflow
Solution 6 - JavaАнтон ЛялинView Answer on Stackoverflow
Solution 7 - Javauser655000View Answer on Stackoverflow
Solution 8 - JavaYounesView Answer on Stackoverflow
Solution 9 - JavaCoDeView Answer on Stackoverflow
Solution 10 - JavaSuragchView Answer on Stackoverflow
Solution 11 - JavaReishinView Answer on Stackoverflow