How can I use one class object in another?


I'll give an example to explain what i need, imagine a Lamborghini database:

CarSeries (int identifier, String name)
CarModel (int modelIdentifier, String modelName, CarSeries (int identifier, String name);

Now I want to create an array where I should be able to directly create a carModel, for example a carModel (123, Gallardo, 345, Superleggera)

So I create the array for three cars:

CarModel carmodels[] = new CarModel[3];

And have to initialize them. Initializing a similar array 0 position for CarSeries is as simple as

carseries[0] = new CarSeries(0,"");

but I can't find the way to initialize a CarModel, and neither to use getters and setters for, for example, set carModel carSeries's identifier.

Update: Based in RAZ_Muh_Taz's code (very helpful, thanks!) I've been able to initialize it! This way: carmodels[0] = new CarModel (0,"", new CarSeries(0,"")); Now i want to set a console output asking for CarSeries identifier, how should code ask for input? How can I use the getter now?

Update2: Finally just created a new array for each Person and just update both arrays simultaneously with same count to point both desired groups of data.

Once you have declared the carmodel array, you have to access to the index of the array to insert each one car models.

CarModel carModels[] = new CarModel[3];

carModels[0] = new CarModel(0, "model1", new CarSeries(0, "serie1"));
carModels[1] = new CarModel(1, "model2", new CarSeries(1, "serie2"));
carModels[2] = new CarModel(2, "model3", new CarSeries(2, "serie3"));

You can also declare the CarSeries before

CarSeries carSerie1 = new CarSeries(0, "serie1");
CarSeries carSerie2 = new CarSeries(1, "serie2");
CarSeries carSerie3 = new CarSeries(2, "serie3");

and use the reference in the constructor of the CarModel

carModels[0] = new CarModel(0, "model1", carSerie1);
carModels[1] = new CarModel(1, "model2", carSerie2);
carModels[2] = new CarModel(2, "model3", carSerie3);

You have to take into account that you have to declare the constructors properly or the code won´t compile