Transcript Slide 1

User Defined Methods
• Methods are used to divide complicated
programs into manageable pieces.
• There are predefined methods (methods that
are already provided by Java)
• The other type are user-defined methods
(methods that you create)
Advantages of Methods
• While working on one method, you can focus on
just that part of the program and construct it,
debug it, and perfect it.
• Different people can work on different methods
simultaneously.
• If a method is needed in more than one place in a
program, or in a different program, you can write
it once and use it many times.
• Using methods greatly enhances the program’s
readability because it reduces the complexity of
the main method.
User-Defined Methods
• User-defined methods in Java are classified into two
categories:
• Methods that have a type – Value-Returning Methods
• Methods that do not have a type, Void Methods
• Value-returning methods
• Used in expressions
• Calculate and return a value
• Can save value for later calculation or print value
• Void Methods
• Used to perform a specific task
Flow of Execution
• Execution always begins with the first statement
in the method main
• User-defined methods execute only when called
• Call to method transfers control from caller to
called method
• In method call statement, specify only actual
parameters, not data type or method type
• Control goes back to caller when method exits
Construction of a Method
• A method must include the following:
• A declaration (header)
• An opening curly bracket
• A body
• A closing curly bracket
• The method declaration (header) contains:
• Optional access modifiers
• The return type for the method
• The method name
• An opening parenthesis
• An optional list of parameters
• A closing parenthesis
Method Headers
• Modifiers: public, private, protected, static,
abstract, final
• returnType: type of value that the method
calculates and returns (using return
statement) OR void if no value is to be
returned. The returnType can be any valid
Java data type.
• methodName: Java identifier; name of
method
• parameterList: Variables (primitive or
reference) that are sent to the method
Syntax: Method
modifier(s) returnType methodName(formal parameter list)
{
statements
}
Syntax
• Parameter List
When creating the parameter list you must define
each variable separated by a comma.
i.e. public static float age (int x, double y)
• return Statement
Every return-type method must use the return
statement to get the value back to the calling
method unless it is a void method.
*A return statement can return only one value.
i.e return expr;
• Argument List
Syntax
The argument list is the listing of values/variables in the method
call.
You must have the same number and type of variables in the
argument listing/actual parameter listing as you have in the
formal parameter listing.
• Method Call
The method is called by using the method name followed by the
argument listing/actual parameter listing.
In a return type method, you must use the method call on the
right hand side of and assignment statement or in some output
statement.
i.e. y = methodName(argument/actual parameter list)
Equivalent Method Definitions
public static double larger(double x, double y)
{
double max;
if(x >= y)
max = x;
else
max = y;
return max;
}
Equivalent Method Definitions
public static double larger(double x, double y)
{
if(x >= y)
return x;
else
return y;
}
Void Methods
• Similar in structure to value-returning
methods
• No method type (i.e. void)
• Call to method is always stand-alone
statement
• Can use return statement to exit method
early
Void Method Example
public class Banner2
{
public static void main (String[] args)
{
printStars();
System.out.println("********** Annual ***********");
printStars();
System.out.println("******* Spring Sale **********");
printStars();
}
public static void printStars()
{
int stars, lines;
for(lines = 1; lines <= 2; lines++)
{
for(stars = 1; stars <= 30; stars++)
System.out.print("*");
System.out.println();
}
}
}
Primitive Data Type Variables as
Parameters
• A formal parameter receives a copy of its
corresponding actual parameter
• If a formal parameter is a variable of a primitive
data type
• Value of actual argument is directly stored
• Cannot pass information outside the method
• Provides only a one-way link between
arguments and parameters
Scope of an Identifier Within a
Class
• Scope (of an identifier): refers to those parts
of a program where the identifier is
accessible
• Local variables: variables declared within a
method (or block)
• Within a class
• Any method can call any other method
• Exception: static method cannot call a
nonstatic method
Scope Rules
• Identifier declared within a method is accessible
• Only within the method from the point at which
it is declared until the end of the method
• By those methods nested within that method if
the nested method does not have an identifier
with the same name as the identifier in the
outside method
*Outside method: method that encloses nested block
Scope Rules: Demonstrated
public class ScopeRules
{
static final double rate = 10.50;
static int z;
static double t;
public static void two(int one, int z)
{
char ch;
int a;
//block three
public static void main(String[] args)
{
int num;
double x, z;
char ch;
{
int x = 12;
//...
}//end block three
//...
}
//...
}
public static void one(int x, char y)
{
//...
}
public static int w;
}
Scope Rules: Demonstrated
Method Overloading: An
Introduction
• Method overloading: more than one method can
have the same name
• Overloading Rules
• Every method must have a different number of
parameters
OR
• If the number of parameters is the same, then
the data type of the parameter (in the order
listed), must differ in at least one position
• Types of parameters determine which method
executes