Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Course Notes
  


Methods page 3 of 37


A method is the object-oriented equivalent of a procedure in nonobject-oriented languages. That is, a method is a program construct that provides the mechanism (method) for performing some act, in this case, barking. Given an instance of some entity, we invoke behavior with a dot syntax that associates an instance with a method in the class definition:

Method Invocation Syntax
<instance>.<behavior>()
<variable> = <instance>.<behavior>(<arguments>...)

To elicit a bark from a dog fido, for example, the operation would be:


fido.bark()

Syntactically, the Java programming language supports passing data to a method and capturing a value returned from a method, neither of which takes place in the previous invocation.

Java is a strongly typed language, meaning that it expects variables, variable values, return types, and so on to match properly, partly because data types are used to distinguish among multiple methods with the same name. Method return types and parameters are specified during definition:

Method Definition Syntax
void <method-name>(<arguments>...) {
<statements>...
}
<return-type> <method-name>(<arguments>...) {
<statements>...
}

Historically, the combination of method name, return type, and argument list is called the method signature. With modern OO languages, a class may define multiple methods with the same name, as long as they are distinguishable by their signatures; this practice is called method overloading. Java has the restriction that return type does not contribute to the method signature, thus, having two methods with the same names and arguments, but different return types is not possible.

For the current example, the return type of void indicates that bark() does not compute any value for delivery back to the invoking program component. Also, bark() is invoked without any arguments. In object parlance, invoking a method relative to a particular object (a class instance) is often called message passing. In this case, the message contains no supplemental data (no arguments).

For now, if we create an instance of Dog, it can bark when provoked, but we have no way of representing data, for example, how many times it will bark, its breed, and so on. Before looking at language constructs that will make the Dog data type more versatile, we must consider a mechanical aspect of the Java language, namely, what's necessary to run a program.


copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact