0

Hi!

I have one small question. Is that possible?

public class test1 {
    public static void main (String[] args){

        test2 client = new test2();
        test2[] call = new test2[1000];

        int x =2;       

        call[1].add1(x);
    }
}




public class test2 {    

    void add1(int x) { 
        x = x+1;
    }
}

Sorry for my bad English.

Edited by Fotis_13

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Fotis_13
1

There are two steps in using an array of objects. The first one defines the array. Your code does that.
The second step is assigning an instance of an object to each element of the array. Your code does NOT do that.
For example:
call[0] = client; // assign an instance of the Test2 class to the first element in the call array.

You would need to assign instances of the class to all the elements that you reference or use.

If call[1] contains an instance of the Test2 class, then the code on line 9 should work.

Edited by NormR1

0

Thank you very much NormR1.

I don't understand why it is need, but it works!

Edited by Fotis_13

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.