[Bluej-discuss] How to call an accessor method in a different class?
Stephen Bloch
sbloch at adelphi.edu
Sun Nov 18 15:16:07 GMT 2007
>If ObjectA has an arraylist of ObjectB, and each ObjectB contains an
>instance of ObjectC which in turn contains an instance of ObjectD, and
>the class that defined ObjectD has an accessor method getName() . How do
>I call that method from within the class that defined ObjectA ?
>...
>Aryeh, thanks for the hint. There is no way that I would have guessed or
>stumbled on that particular syntax.
Aryeh is quite correct that this list isn't really intended for
student questions about Java. However, I want to comment on this.
One SHOULD be able to "guess or stumble upon" the syntax to do this,
step by step. How do you refer to ObjectA? Next, how do you refer
to its arraylist of ObjectB's? Next, how do you refer to an
individual one of those ObjectB's? Next, how do you refer to the
ObjectC in that ObjectB? Next, how do you refer to the ObjectD in
that ObjectC? Next, how do you call getName on that ObjectD? Each
one of these questions should be easy to answer by itself, as long as
you don't try to answer all six at once.
This might be a little easier if the first question or two had less
magic in them -- in particular, the automatic "this" shortcut. When
I teach this stuff, I expect EVERY field reference to be of the form
OBJECT.FIELDNAME, and EVERY method call to be of the form
OBJECT.METHOD(ARGS); if the field or method is in the current object,
my students are expected to use "this" explicitly as the OBJECT.
--
Dr. Stephen Bloch
Math/CS Department
Adelphi University
More information about the bluej-discuss
mailing list