A method

advertisement
Methods
Within a class, a method is a segment of code, which is set apart in a special way and
designed to accomplish a task. When it is called (calling will be discussed later) or
invoked, program control passes to the method. When invoking a method,
information can be passed to it, which can be used during its execution. When the
method is executing, execution of the return (a keyword) statement causes control
to return to the point immediately following the call to the method. There can be
more than one return statement.
Be familiar with the following:
 Methods are used to break a larger problem into smaller more manageable
pieces, which can then be used to construct the solution of a larger problem.
 Methods can be called from multiple places within a program solution. In
other words, the code is written once and called multiple times, thus
reducing the amount of coding required.
 Normally, a method is designed to accomplish a single task. As the number of
tasks performed by a method increases the ability to call it from multiple
points in a program is reduced. Side effects should be minimized. For
example: Suppose a method to compute the volume of some geometric
figure is needed. Then it’s probably not a good idea also to print the
computed volume within the method, as this renders it unusable at a point
where only the volume is needed and no output is desired.
 Methods may perform a task, return a value, or do both of these.
Methods are defined by using the following components:
[modifiers] [return type] [name] ( parameters) { //this is the method header
method code
}
modifiers: The modifiers that we will begin with are:
 public – accessible inside and outside the class
 private – accessible within the class
 static – can be called directly, without the instantiation of an object.
(Example: Math.sin(x) Here, Math is the class and not an object.)
return type: A method (with the exception of a constructor, which we will discuss
later) can return a single value or object. The type of this return must be specified.
If it does not return anything, its return type is void, again with the exception of
constructors.
name: This is the identifier that will be used to call the method. It is a user-defined
identifier that follows the standard rules for creating symbols. The generally
accepted convention (not a Java rule) for naming methods is to start with a lowercase symbol. Each successive word in the name begins with a capital letter.
Parameter list: This is a list of variables which will receive information from the
calling statement.
method code: The set of code that will be executed during the run of the method.
Examples of methods we have seen (showing the call statement)
 keyboard.nextLine()
 keyboard.nextInt()
 System.out.println()
 System.out.printf(String, argument list)
 str1.toUpperCase()
 myRan.nextDouble()
Example:
Suppose we want to write a static method that will compute the volume of a sphere
of radius r. Since it is static we would be able to call it without creating an object in
the class. For illustration let’s suppose that this method is being placed inside a
class named Geometry.
Its return type will be double, since spherical volume is seldom an integer value. It
will need one parameter to which we can supply the value for the radius. Recall that
4
the volume of a sphere of radius r is 3 𝜋𝑟 3 .
public static double volumeSphere(double r){
double v; //declare a local variable to hold volume
v = 4.0/3.0*Math.PI*r*r*r;
return v;
//return the volume of the sphere
}
Make sure you can identify the modifiers, the return type, the name, the parameters,
and local variables
We could have accomplished this without declaring the local variable v, in the
following way:
public static double volumeSphere(double r){
return 4.0/3.0*Math.PI*r*r*r;
}
This could be called inside the class Geometry by writing
volumeSphere(some double value or variable)
From outside the class it can be called by writing
Geometry.volumeSphere(some double value or variable)
Method Signature and Method Overloading
Within a project, there may be more than one method with the same name, as long
as the parameter lists are all different. This is accomplished via the method
signature, which is a unique compiler-generated identifier of the method. The
compiler computes the signature using both the name and the parameter list of the
method. We’ve already seen an example of this when we used calls like:
myRan.nextInt();
myRan.nextInt(100);
Both have the name nextInt, but one has no parameters and the other has one
integer parameter, and of course, they do different things. The ability to have
multiple methods with different names is called method overloading.
Question: Why would overloading not work based on return type?
Question: Do you think a method can call itself?
Download