views:

50

answers:

5

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?

EDIT:

in regard to Mark's answer here is my follow up question:

is there a shorter way to assign values to the instance variables of an object in the array of objacts than this: (without writing any special constructors)

    Z[1] = new TestAO() ; 
    Z[1].x[4] = 80085 ;
+4  A: 

It's easier than you think:

Z[1] = new TestAO(); 
Mark Byers
if i do this though then there is't any reference to the instance of TestAO though; there's no 'a' or 'b' or 'c' or whatever. How do i refer to it in a way that doesn't include a reference to the array Z? or do i not need to worry about that?
David
Just declare a reference and have it point to Z[1], or simply refer to that variable from there on as Z[1]
Amir Afghani
@David: Are you worried that the object might be garbage collected if you don't have an explicit reference to it? Don't worry: it won't be.
Mark Byers
Maybe you are a little bit confused with references to objects. Z[1] is just a reference to a object. When you write TestAO b = Z[1] you are pointing to the same object. So essentially no need for "b". You can always use Z[2].doWhatYouWantWithObject().
Petar Minchev
i think my edit to my question voices my follow up question the best.
David
A: 

What you're really doing here is assigning the result of an assignment to Z[1]. The return type of an assignment in Java is boolean, so the way you're doing it is not going to work.

Try:

Z[1] = new TestAO();
Syntactic
+1  A: 

Declaring variable like this is impossible. Just write "Z[1] = new TestAO();" and if you want another reference "TestAO b = Z[1]";

Petar Minchev
A: 

Try this:

Z[1] = new TestAO() ;
Amir Afghani
A: 

You're simply using invalid syntax. You can't declare the variable b this way. Try:

TestAO b = new TestAO();
Z[1] = b;

In general, I'd advise against trying to write lots of compound statements like this. They only make your code more confusing.

Peter Ruderman
i already had a decleration like that in the original code in the question for 'a'
David
Perhaps I misunderstood your question. If all you want to do is assign the result of an assignment to an array index, then you certainly can: Z[1] = (b = new TestAO()). As I tried to explain, the problem comes when you try to declare b on the same line. In any case, don't be clever, be clear.
Peter Ruderman