For an assignment I need to create a class within class Point from java.awt.Point. I need to be able to use point methods while creating my own variables for the inner/nested class. I am a bit confused by the differences between static nested classes and inner classes and dont know which one to use. I have tried to use a static nested class and have encountered an error stating “modifier static not allowed here”
My misguided attempt:
class Point {
static class RobotJAW {
int goldcollected, x, y;
RobotJAW() {
goldcollected = 0;
alive = true;
x = 0;
y = 0;
}
}
}
The point of creating this class it to have a robot search a field of gold and bombs and collect gold while being destroyed by bombs. I can only use the Point and Scanner classes from the java API. I need to use the point methods equals(obj), move(int x, int y), getLocation(), and setLocation(int x, int y). But I need to add goldcollected and alive variables. Also, this is an assignment for school, so I would like to gain some more knowledge about the subject, and some guidance, without being given someone elses work.
A lesson about how inner classes work, then some doubts that that is what you really need.
An inner class is used for describing a class with an extremely strong belongs-to relationship to another class. So strong infact, that it cannot survive without the outer class. So strong, that you rarely need it. The inner class is only accessible through an instance of the outer class:
A static inner class is not really an inner class. It is just a class that is defined inside of an inner class.
It looks to me like what you want is for RobotJaw to either be a point, or have a point. You don’t want it to be an object unattachably contained by a point. So either have RobotJaw extend a Point, or contain a point.