0

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"?

chameerar
  • 322
  • 1
  • 2
  • 8
  • 1
    Because each index of the array (memory address) is its own pointer back to the `a` reference. You should take a look at pointers in the C language to get some historical background. Java hides the pointer logic. – Mr. Polywhirl Jul 14 '23 at 14:11
  • 2
    "variables referencing"? Two: `a` and `b`. How many *references* are there to `Car` objects? 6: `a`, `b`, `cars[0]`, `cars[1]`, `cars[2]` and `cars[3]`. References can exist in local variables, fields, as array members or as method parameters. – Joachim Sauer Jul 14 '23 at 14:12
  • Are cars[0],cars[1],cars[2],cars[3] reference to "a", not to Object Car() ? cars[0-3]-->a-->Car()<--b – ImNotARobot Jul 14 '23 at 14:20
  • Please note that in Java, Array is completly different with List. Inside List you can add/remove item but Array cannot anymore change length after because each index is its own pointer to new reference like saying @Mr.Polywhirl – elgsylvain85 Jul 14 '23 at 14:24
  • @ImNotARobot if you reassign / change `a` after you initialize the `{a,a,a,a}` like `a = new Car("BMW");` this will not have any effect on your array and the array elements will still point to the old Car objects and not the new "BMW". That should show you that all of those are separate reference variables. (This is because java [is pass by value](https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value)) – OH GOD SPIDERS Jul 14 '23 at 14:26

1 Answers1

1

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.

1 class Car {}
2 
3 class Example {
4     public static void main (String[] args){
5         a=new Car();
6         b=new Car();
7         Car[] cars ={a,a,a,a};
8     }
9 }

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):

enter image description here

Ash
  • 111
  • 6
  • Clarified the answer with the code. It feels like I can't understand one thing in any way and it ruins all my logic ( – ImNotARobot Jul 14 '23 at 15:12
  • @ImNotARobot using Ashs diagram above think of it like that: Doing something like `a.model="KIA"` you are changing the existing Car object (The car square). And because all References point to that one single object that change will seen in all those places. But when you do `a = new Car()` you are creating a new Car object and point `a` to that. So in above diagram you would create a second "Car" square, then remove the arrow from a to the old car object and connect it to the new object. But of course all the other references / arrows still point at the old object – OH GOD SPIDERS Jul 14 '23 at 15:36