Within an aray of objects can one create a new instance of an object at an index?
- by David
Here's the sample code:
class TestAO
{
int[] x;
public TestAO ()
{
this.x = new int[5] ;
for (int i = 0; i<x.length; i++)
x[i] = i;
}
public static void main (String[]arg)
{
TestAO a = new TestAO ();
System.out.println (a) ;
TestAO c = new TestAO () ;
c.x[3] = 35 ;
TestAO[] Z = new TestAO[3] ;
Z[0] = a ;
Z[1] = (TestAO b = new TestAO()) ;
Z[2] = c ;
}
}
When i try to compile this i get an error message at the line Z[1] which reads as follows:
TestAO.java:22: ')' expected
Z[1] = (TestAO b = new TestAO()) ;
^
What i'm trying to do here is create an instance of the object TestAO that i want to be in that index within the assignment of the value at that index instead of creating the instance of the object outside of the array like i did with a.
Is this legal and i'm just making some syntax error that i can't see (thus causing the error message) or can what i'm trying to do just not be done?