-1

Given the three example classes:

public class MainClass {
    public MainClass() {
        ArrayOfObjectA arrA = new ArrayOfObjectA();
    }
}

,

public class ArrayofObjectA {
    ...
    public void doSomething() {
        ...
    }
}

, and

public class ObjectA {
    ...
    public void doThis() {
        arrA.doSomething(); // <-- Here's the issue
    }
}

How would I call doSomething() from arrA?

Additionally, arrA holds an array of ObjectA objects, making this problem even more complicated.

I made arrA a class object in MainClass and then had a getArrA() method in there, but I feel like there is a better solution to this.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
null
  • 2,060
  • 3
  • 23
  • 42
  • 4
    What would that even mean, for the object to call something on...the array containing it? An array it contains? – Louis Wasserman Apr 29 '15 at 22:10
  • The purpose of this is to allow `ObjectA` to manipulate the other `ObjectA` objects in `arrA`. Since `arrA` contains the array of the objects, it can directly call the methods of each `ObjectA` object in the array. Therefore, if an `ObjectA` object could call that method, it would be able to indirectly call the methods of the `ObjectA` objects. Plus, the method will perform in a specific way depending on which `ObjectA` object calls `doSomething()`. – null Apr 29 '15 at 22:16
  • Sounds like `doThis` should accept the `ArrayofObjectA` as an argument. – Louis Wasserman Apr 29 '15 at 22:18

1 Answers1

2

ObjectA needs a reference to arrA. Typically this is done by passing it as a parameter to that function:

public class ObjectA {
    ...
    public void doThis(ArrayofObjectA arrA) {
        arrA.doSomething(); // <-- Here's the issue
    }
}

By doing this, you ensure that a reference is 'in scope' for that function. Normally functions can only 'see' parameters passed to them and member fields of their class. So, the other way it can call doSomething() is if there is a reference in the class itself:

public class ObjectA {
    private ArrayofObjectA arrA;

    public ObjectA(ArrayofObjectA a) {
      this.arrA = a; ///'this' is optional but makes it clear we are talking about a member field.
    }

    public void doThis(ArrayofObjectA arrA) {
        this.arrA.doSomething(); // <-- Here's the issue
    }
}

Note that a 'reference' is essentially some handle that allows you to refer to a particular instantiation of an object. You need a reference to call any non-static method. See this answer for more information on the difference between those two things.

Also note that despite naming your class ArrayofObjectA, it is not actually an array. There is only one instance of it declared (in your MainClass) and it (currently) possess no references to any ObjectA instantiations. More properly you might name this ArrayHolder:

public class ArrayHolder {
    private ObjectA[] arrA;//note that the [] indicates it's an array.

    public ArrayHolder() {
        this.arrA = new Object[10];//Length of ten
        for (int idx=0; idx<10; idx++) {
            this.arrA[idx] = new ObjectA(this);//pass new ObjectA a REFERENCE to 'this' object (ArrayHolder)
        }
    }

    public void doSomething() {
        //This is now callable from all ObjectA objects.
    }
}
Community
  • 1
  • 1
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • Wouldn't the second way just cause each ObjectA object to store an array of ObjectA objects including itself? That would be more memory heavy than going the getter-setter route. – null Apr 29 '15 at 22:22
  • No: it's not storing an array, it's storing a reference to an array. A reference you need in order to have access to that array. – Nathaniel Ford Apr 29 '15 at 22:25
  • Oh that's right, I see. Then it seems as though the second way would be the easiest to implement. Thanks. – null Apr 29 '15 at 22:26