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.

Recommended Answers

All 2 Replies

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.

Thank you very much NormR1.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.