I have difficulties understanding how to correctly count a reference variables.
Example (adopted from some book for java learning):
class Car {
}
class Example {
public static void main(String[] args) {
a = new Car();
b = new Car();
Car[] cars = {a,a,a,a};
}
}
How many reference variables does class Car() have (variables referring to it)?
My answer is 2. There is only two variables "a" and "b" referring to object Car(). But correct answer is 6. Why are variables a counted as separate references inside the list? Isn't it same reference "a"?
I've added line numbers to your code to ease explaining what happens. This answer combines what Mr. Polywhirl and Joachim Sauer said in comments.
For this type of exercises (for example on an OCP exam) it can also help to draw out what happens using boxes and arrows.
Program execution starts on line 4. On line 5 the first reference to Car is made and stored in variable a. On line 6 a second reference to Car is made and stored in variable b.
On line 7, the Cars[] array is created, which is a new object containing the value of a four times. This creates four new references to Car stored in cars[0] through cars[3].
Drawn out it looks like this (corrected, based on OH GOD SPIDER's comment):