Reputation: 2472
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.
Upvotes: 0
Views: 240
Reputation: 1843
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
.
Upvotes: 2
Reputation: 2576
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.
}
Upvotes: 1