Strange for loop

I use this bottom code for my project, but I have a question about the for(Car car: cars) loop. I've been trying too find an explanation for this for loop but haven't got any results so far. Why does the for loop first create an object? I assume the arraylist cars in this for loops just sets the end of the for loop.

But imagine that I want to use the car class again. I can't use the object created by new Car(4,5,4) right?

No you cannot "use" the car as you have thrown away the reference to it. Remember that the new operator creates an object by calling the constructor and then returning a reference to that object. Therefore you need to store it somewhere.

Perfectly alright. It compiles, it runs but as you have ascertained not very useful in certain situations.

p.s. i still dont really understand what the for(Car car: cars) does

It is called the enhanced for loop. Alternatively called the for each loop. Basically it is saying "for each object in the list....". So if you have a List or an array or some other iterable data structure the loop will execute N times where N is the number of objects in the data structure.