32

I'm new to programming and I'll be studying it next year in uni. In my public static void main ... I can't create a new SimpleCircle. This error occurs only on my circle. Thank you very much for the help! :)

public class TestSimpleCircle {

    class SimpleCircle {
        double radius;

        SimpleCircle(){
            radius = 1;
        }

        SimpleCircle(double newRadius){
            radius = newRadius;
        }

        double getArea() {
            return radius * radius * Math.PI;
        }

        double getPerimeter() {
            return 2 * radius * Math.PI;
        }

        void setRadius(double newRadius) {
            radius = newRadius;
        }
    }

    public static void main(String [] args) {
        SimpleCircle circle = new SimpleCircle();
        System.out.println("the area of the circle of radius "+circle.radius+" is "+circle.getArea());

        SimpleCircle circle2 = new SimpleCircle(25);
        System.out.println("the area of the circle of radius "+circle2.radius+" is "+circle2.getArea());

        SimpleCircle circle3 = new SimpleCircle(125);
        System.out.println("the area of the circle of radius "+circle3.radius+" is "+circle3.getArea());

        circle.radius = 100;
        System.out.println("The area of the circle of radius "+circle.radius+" is "+circle.getArea());
    }
}
teppic
  • 7,051
  • 1
  • 29
  • 35
Nick Ninov
  • 317
  • 1
  • 3
  • 5

2 Answers2

69

You declared you SimpleCircle class as inner class for TestSimpleCircle. You need either move it into a separate file or declare it as

static class SimpleCircle
Ivan
  • 8,508
  • 2
  • 19
  • 30
  • My problem was not reloading the application in tomcat! It needed to reload the class files with the updated inner class as static. Not sufficient to just replace the server files - have to reload application :) – Skystrider Mar 11 '20 at 21:37
  • 1
    @Skychan, what your comment has to do with the answer or the question? Wrong window? – Ivan Mar 11 '20 at 22:10
  • Good point, my comment could be better on the question or as it's own "solution". I don't mind moving it, I guess I just figured I had a different situation than the OP but had exact same error so I wanted to help others who come here for same reason as me, they may see my silly problem/answer because everyone looks at the accepted answer first. I'll move it if you advise. – Skystrider Apr 21 '20 at 15:46
33

SimpleCircle is an inner class of class TestSimpleCircle. This means that you first need an instance of object of enclosing class, for example:

TestSimpleCircle tsc = new TestSimpleCircle();

Now you are able to create an instance of inner class that is connected with an instance of enclosing TestSimpleCircle class:

SimpleCircle sc = tsc.new SimpleCircle();

As you see, to create an instance of object of inner class you had to specify to which exactly object of enclosing class you want it to belong (with the tsc.new in your example).

If you need to create an instance of SimpleCircle without instance of object of enclosing class you should have declared this class as static class SimpleCircle{code of your class here}

Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21