I got 2 classes.
- ToyCar
- ToyShop
ToyShop has a toyCar field which accepts a ToyCar object.
public static void main(String[] args) {
ToyCar[] cars = new ToyCar[3];
cars[0] = new ToyCar();
cars[1] = new ToyCar();
cars[2] = new ToyCar();
ToyShop company = new ToyShop();
company.setToyCar(cars[2]);
cars[0] = null;
cars[1] = null;
cars = null;
print(company.getToyCar())
}
If I run the program, will the cars array be garbage collected, or just cars[0] and cars[1] is collected??
If the array is garbage collected, can I print out toyCar in company??
If my question does not make sense, please point out.
carscould probably be collected (if there are no other references to it that we don't see) but since the application would stop at that point anyways the question is a little moot.cars[2]will still be available? What happened when you tried it? What do you think should happen? Would it be reasonable to GC something that still has a reference?