Method:
example
You will have gained experience and
understanding of the sending of messages between objects, and the invocation
of methods corresponding to received messages. What you may have not realised
is that an object can send a message to itself, simply by a statement that
consists of a message, but no explicit object to send the message to.
We shall investigate such issues in
detail in a later unit, however, for illustration we shall give a simple
example to show the deviation from the default, sequential flow of statement
execution.
Consider this pseudocode design for
part of a class:
class
UnconditionalBranch
{
method2()
{
statementM1;
statementM2;
}
method1()
{
statementA;
method2();
statementB;
}
// other methods
...
}
When method1 is being executed, the order
that statements will be executed will be as follows:
statementA;
statementM1;
statementM2;
statementB;
since statementA
is executed first, followed by the sending of the message method2().
Since no object is specified for the object to be sent to, this message
will be sent to the object that is the instance of the class currently
having its method2()
method executed. The run-time interpreter will see that this class defines
a method2()
that corresponds to the. The run-time interpreter then transfers control
of execution by invoking method method2()
?i.e. method2()
will now have its statements
executed. Next statementM1
is executed, followed by statementM2.
That ends execution of method2(),
so control of execution returns to the place where the message was sent
in method1().
Next statementB
is executed. Then method2()
completes its execution (and control of execution returns to whatever happened
before method1() was invoked.
A program listing of a class UncontionalBranch
(the listing is UnconditionalBranch.java) is
available to examine, compile, modify etc. if you wish to explore these
issues now. Although a later unit will present a more detailed investigation
of the relationship between messages, classes, objects and methods.
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|