Method invocation (subroutines) in Java
When we write a statement that sends a
message to an object, the run-time interpreter has to work out if the message is in the
object's protocol, and if so, where in the class hierarchy the appropriate implementation
of the corresponding method can be found. Once found, the run-time interpreter will
execute the instructions of the method, and then return execution to the line following
the one where the original message was sent.
Unlike selection and iteration there is no
testing taking place to decide whether on not the message will be sent. Thus this kind of
branching is called unconditional branching.
In the example programs you have met already,
you have already seen many examples of message sending, resulting in the invocation of
methods. The class System and its corresponding println method is one of the (many) built-in
messages/methods on the Java language. When a program sends a println message it must indicate what to print, and to which object
to send the message:
System.out.println
("hello")
or
System.out.println
(2 + 2)
In these examples "hello" and
"2 + 2" are arguments that form part of the message, and that become inputs to
the invoked println method. When this method was implemented, by the Java development
team, they defined how that arguments are processed. When you define methods in your
programs, you will have to do the same.
A note on 'built-in' methods
In fact methods such as println are not really built-in to the
language itself. Technically they are built into the standard class library. However, that
distinction is not important here.
A note on methods and subroutines
In programming languages that are not
object-oriented something similar to method invocation is achieved through subroutines. A
subroutine is a mechanism for the execution of a series of instructions elsewhere in a
program, then returning to the original point. The whole idea of subroutines is considered
rather old-fashioned these days, although some, usually much more sophisticated,
equivalent can be found is the majority of modern programming languages.
.Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|