Java OOP basics

后端 未结 1 1752
闹比i
闹比i 2021-01-26 03:24

I have a problem that keeps stalling me from advancing further, this error is not logical at all in my opinion , I am learning from a book and the code is from there. This is t

相关标签:
1条回答
  • 2021-01-26 03:49

    You defined SimpleCircle as an inner class, that is an unnecessary complication here and it is what is keeping this from compiling. Move the SimpleCircle class declaration out from inside the declaration of clzzz and you'll fix the problem.

    Alternatively you could make SimpleCircle a static inner class by adding the static keyword. If you keep this as a static inner class then, if you can use it outside of clzzz, you'll need to refer to it using the outer class as well as the inner class (clzzz.SimpleCircle) so that the JVM can find it.

    Typically static inner classes are used for organizational purposes, because you have something you use along with another class but it doesn't depend on it (see java.util.Map.Entry for an example, although it's an interface and not a class).

    static doesn't mean you can have only one instance. In the context of a class definition it means that there is no dependency on an instance of the outer class. You still can create multiple instances using the static inner class (again, think of Map and Map.Entry, where you can iterate through all the entries of the map, each one is a separate instance of Map.Entry). You can think of static as meaning, "you don't need an object instantiated from the class where this thing is defined."

    Making something a non-static inner class means objects of the inner class are accessing things in the scope of the outer class' instance, so you can't create an instance of the inner class without a reference an instance of the outer class, and that's what the compiler is complaining about.

    0 讨论(0)
提交回复
热议问题