Java – Inner classes as part of outer classes

Inner classes as part of outer classes… here is a solution to the problem.

Inner classes as part of outer classes

Let’s say I have a toy store. I have a web page where I can buy these toys. Each toy has its own characteristics, but to represent a human toy, I have a class called HumanToy that has some properties like height, weight…. But I have a nested property, which itself is a JavaBean > HumanLegs class called this class has its own characteristics, such as:

public class HumanToy {     

private Double height;
    private Double weight;

private HumanLegs humanLegs;

private class HumanLegs {

private Double height;
        private Double weight;

My question is:

Does it make sense for this toy to have a static HumanLegs class? I mean, conceptually, HumanLegs can’t exist alone, they only exist in toys, so I think it’s first an inner class (not a separate class) and secondly not static for the same reason, I mean, only by instantiating a HumanToy you can get a HumanLegs object or get a HumanLegs object HumanLegs object You need a HumanToy object. Is this the right idea?


Yes, this makes sense, since HumanLegs keeps references to its outer classes.
If you move an instance of HumanLegs to another instance of HumanToy, the original HumanToy will not be deleted because HumanLegs does retain a reference to it.

Therefore, the outer class remains in memory at least as long as all instances of the inner class.

Related Problems and Solutions