0

So, granted I'm pushing the envelope of my limited Java knowledge. I would like to have an array of objects within a class. I thought this was as subclass but that's not what I want. I don't know what I want. Here is the java docs example.

public class Bicycle {

    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;

    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }

    // the Bicycle class has four methods (getters/setters removed)

    // here is my new thought - not sure if its right
           public class DriveChain {
              public int big-chainring
              public int little-chainring
              public int chain 
        // and getters and setters
           }
    // here i want to create an array of this.
    ArrayList<DriveChain> dcArray ;

    // here i can add to the array
        public void addDriveChain(drivechain dc) {
            this.dcArray.add(dc);
        } 
}

I want to add fields with getters and setters within this class and treat it as an array list. For example as above. Hope I am making sense.

5
  • 2
    public void addDriveChain(DriveChain dc) { if(dcArray == null) { dcArray = new ArrayList<DriveChain>(); } this.dcArray.add(dc); } Commented Aug 6, 2014 at 21:03
  • Do you want to create something like a C style structure? Commented Aug 6, 2014 at 21:15
  • 2
    @Zhuinden dcArray should be final and initialized to an empty list. That avoids the needless complexity of your snippet. Commented Aug 6, 2014 at 21:16
  • We need more clues as to what you're trying to accomplish. How is the above code not meeting your needs? Commented Aug 6, 2014 at 21:18
  • 1
    The analogy to keep in mind is "HAS A" vs. "IS A". If you would describe something as "IS A" you want a subclass ("A Bicycle IS A WheeledVehicle."). If the relationship is "HAS A" you want a separate class that doesn't inherit ("A Bicycle HAS A DriveChain"). Whether the DriveChain is an inner class or not will depend on the overall design of your system (do other things use DriveChain? How big is the Bicycle class file?). Commented Aug 6, 2014 at 21:24

2 Answers 2

2

I don't see a reason why you wouldn't put DriveChain in its own file. If you keep it as a nested class within Bicycle, then make sure it is a static class to avoid capturing the enclosing instance.

Other than that, your approach makes sense, just use it with this little change:

private final List<DriveChain> driveChains = new ArrayList<>();
  • collections should mostly be final and initialized to an empty collection because they are mutable anyway, but this eliminates the danger of NullPointerException;

  • use List as the variable type, not ArrayList (programming to the interface instead of implementation);

  • don't use "array" as part of the variable name, since it is not an array. Use dcList or, as I would, driveChains.

Sign up to request clarification or add additional context in comments.

2 Comments

Note to OP: final is not required for your code to work - but it prevents you from making some types of mistakes in the future.
Thanks, this is what I wanted, I didn't figure out that you can call classes from other classes. I just wasn't putting two and two together, I put this code in and it works great.
1

Java will not allow more than one public class in one .java file. So you can do what you want to by declaring private inner classes for DriveChain and other Bicycle parts as you need, which makes sense if Bicycle is the only class that will use these components. Food for thought is will you be designing a Motorbike class tomorrow which might use an enhanced version of DriveChain.

If that is true you're better off making public classes for each of the components as well.

If not I would follow the following pattern:

public class Bicycle {
    //declare all component classes first
    private class DriveChain {
    //impl
    }
    private class Handlebars {
    //impl
    }
    .....

    //Form private/public members from the already declared components 
    private int cadence;
    private int gear;
    private int speed;
    private ArrayList<DriveChain> dcArray ;   
    private Handlebars handlebars ;


    //Init each in constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
         gear = startGear;
         cadence = startCadence;
         speed = startSpeed;

         //including the non parametrized ones
         dcArray = new ArrayList<DriveChain>();
         handlebars = new Handlebars();
    }

    //member functions that can now access any of the member fields and their internal functions as well.


}

1 Comment

Why would you create the dcArray and handlebars instances in the constructor? As you state in the comment, they are "non parameterized".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.