Within an aray of objects can one create a new instance of an object at an index?

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-03-23T18:41:10Z Indexed on 2010/03/23 18:43 UTC
Read the original article Hit count: 238

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about array-of-objects