Question

I've just started working through the Java tutorials again and I decided to spend some time playing with nested classes.

The working copy I've made is a Bicycle class which could have zero or more Rider class instances within it. From within an instance of Bicycle, I'd like to be able to work with instances of Riders.

In the example below, my Bicycle has an ArrayList which holds its Riders, but I'd like to avoid having to maintain this list if possible.

My question is this. Is there another way to reference instances of Rider from Bicycle, or do I have to maintain my own list?

//Outer Class contains zero or more Inner Classes
public class Bicycle {

    //ArrayList of Inner Class instances
    private List<Rider> Riders = new ArrayList<Rider>();

    class Rider {
        private String riderName;

        protected Rider(String newRiderName) {
            this.riderName = newRiderName;
        }

        protected String GetRiderName(){
            return riderName;
        }

    }

    //When creating an Outer Class, we can instantiate zero or more Inner Classes, by name
    public Bicycle(String... startingRiderName) {

        //Loop through all Inner Class instance names and create an Inner Class for each
        for (String newRiderName : startingRiderName) {
            Riders.add(new Rider(newRiderName));
        }
    }

    //Public method to return an array of Inner Class instance names
    public String[] GetRiderNames() {

        String[] riderNames = new String[Riders.size()];

        // Just in case we mess up and ask the ArrayList for an out of bounds index
        try {
            for (int riderIndex = 0; riderIndex < Riders.size(); riderIndex++) {
                riderNames[riderIndex] = Riders.get(riderIndex).GetRiderName();
            }
            return riderNames;
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }

}
Was it helpful?

Solution

A list has to be maintained somewhere, otherwise those inner class instances will be garbage-collected. Whether that's in an array that is a member of Bicycle or in some external data structure is a design decision, but there's no automatic list kept by Java of inner class instances.

OTHER TIPS

Yes you will need to maintain a list.

Also, a Rider doesn't seem like it should be an inner class of Bicycle. By making Rider an private inner class you are saying that it is only ever useful to Bicycle. Conceptually an inner class is more for some sub-part of Bicycle that performs an internal operation on or holds internal data that composes and is specific to that bicycle. I could imagine a FlatTyreEventHandler being an inner class, but I don't think of the Rider as part of the internal fabric or operation of a Bicycle.

Now, having a Rider and internal class of Bicycle might work in your instance, but bear in mind that anything the outside world wants to know about a rider will have to go through Bicycle, so you will have to keep adding methods such as GetRiderAge(), GetRiderRoutePreference(). These methods are specific to a rider and have nothing to do with a Bicycle. If you find yourself doing this, then Rider is not really an internal hidden class, but instead the methods on Bicycle are just passthroughs to Rider methods and having it as an internal class serves little purpose. Also, the Bicycle has lost its single purpose (of being a Bicycle) and is bloated out with a Rider interface.

So, I would probably make Rider a stand-alone class and then Bicycle has a list of Riders. Alternatively, you can have a separate class which is BicycleRiders and this class can hold a map from bicycles to riders.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top