I assume those are bacics.
I’ve created 3 objects like this:
for (int j = 1; j < 4; j++) {
int parkingSlot= 1 + rd.nextInt(3);
AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
}
Based on class AircraftCarrier (it’s constructor):
public AircraftCarrier (FlightControl fc, int idC, int parkingSlots, int freeParkingSlots) {
this.kontrolaLotow = fc;
this.id = idC;
this.ps = parkingSlots;
this.fps = freeParkingSlots;
}
So i have 3 Aircraft Carriers, right? Let’s assume I need to change freeParkingSLots value for a carrier with id =2. How do I do that?
You created three instances but since you didn’t maintain the reference to any of them, you no longer have them. They have been sent to garbage collection.
You need to store each instance in some collection for later access.