0

I'm struggling to understand what I consider a fundamental question in Java. Below the answer is given that line 3 leans to a class cast exception. I can see that originally a1 pointed to an array of object A. But on line 1 doesn't a get set to point to an array of object b? And therefore if a1 was pointing to a should it now not also point to b?

Explanation from Enthuware:-

The program will throw a java.lang.ClassCastException at the line labelled 3 when run.

The line //1 will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference. The cast in line //2 is needed because a superclass reference is assigned to a subclass reference variable. And this works at runtime because the object referenced to by a is actually of an array of B. Now, the cast at line //3 tells the compiler not to worry, that I'm a good programmer and I know what I am doing and the object referenced by the super class reference (a1) will actually be of class B at run time. So there is no compile time error. But at run time, this fails because the actual object is not an array of B but is an array of A.

public static void main(String args[]) {

    A[] a, a1;
    B[] b;
    a = new A[10];
    a1 = a;
    b = new B[20];
    a = b;         //line 1
    b = (B[]) a;   //line 2
    b = (B[]) a1;  //line 3

    }

}

class A {
}

class B extends A {}

2 Answers2

1

And therefore if a1 was pointing to a should it now not also point to b?

No.

Variables hold values and are completely independent of other variables. If you change the value of one variable, you've only affected that variable.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks for your answer. Both of you have been helpful but I think David SN's explanation expands on points I was unclear on. –  Feb 06 '15 at 17:20
0

If there are multiple variables referencing the same object, changes in the object can be done using any of the references, but changes in the references only affect that reference.

In your example, a and a1 reference the same array. If you modify the referenced object, the array of A, it's the same if you use a or a1 because they make reference to the same object.

A[] a, a1;
a = new A[10];
a1 = a;
/* You can use a or a1 to modify the object with the same results. */
a[0] = new A(); /* Equivalent to a1[0] = new A() */

If you modify the variable to reference another object, the other reference remains unchanged.

A[] a, a1;
a = new A[10];
a1 = a;
a = new A[15]; /* a1 still references the first array and a the new one */

Something similar happens when you use a object reference as a function parameter.

To make an example, given than method:

public static void uselessMethod(A[] a) {
    /* a is a local variable that makes reference to the object,
       modify the reference only has effect inside this method,
       but modify the array has effects outside the method */
    a = null;
}

If you call the method above, your reference don't change:

A[] a = new A[10];
uselessMethod(a);
System.out.println(a.length); /* This will print 10 */
David SN
  • 3,389
  • 1
  • 17
  • 21