how can I access the getStick() method defined within the chopStick class
To access any part of an instance of a class, you need a reference to that instance.
Some where you create an instance:
AnObject aRef= new AnObject(); // create an instance and set reference to that object
then you need to make that reference available where you want to call its methods:
aRef.aMethod(); // call a method in the AnObject class
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
I did. That's what the following is:
aRef.aMethod(); // call a method in the AnObject class
You need a reference (aRef) to an instance of the class(AnObject) to call one of its methods(aMethod)
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Pass the reference in the constructor or via a setter method.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
AClass1 anObj = new AClass1(); // create an instance of AClass1
AClass2 anObj2 = new AClas2(anObj); // pass reference to AClass1 via constructor
...
anObj2.setReference(anObj); // pass reference via a setter method
After either of the above, the AClass2 object will have a reference to the AClass1 object.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Here are two ways that I suggested to pass a reference to a class. The same can be used to pass the reference to any number of classes.
AClass2 anObj2 = new AClas2(anObj); // pass reference to AClass1 via constructor
...
anObj2.setReference(anObj); // pass reference via a setter method
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656