Chapter 6 Methods Chapter 6 - Methods
Methods A method is separate piece of code that can be called from another method to perform a specific function. Each method must be defined within the body of a class. The method is called by naming both the method and the object that it is defined within in an expression, together with a list of parameters in parentheses. Chapter 6 - Methods
Example Both methods defined within class SquareInt Call to method square from method main Definition of method square Chapter 6 - Methods
Why Use Methods? Independent testing of sub-tasks. Reusable code. Design and test a method once, and re-use it whenever you need to solve a similar problem. Isolation from unintended side effects. The only variables from the caller that can be seen from a method are those in the argument list. Chapter 6 - Methods
Method Definitions Return type Method name Arguments Keyword(s) Method body Returned value Chapter 6 - Methods
The Pass-by-Value Scheme Arguments are passed to Java methods by value, meaning that each argument is copied, and the copy is passed to the method. If an argument is modified within the method, it modifies the copy, so the value in the calling method is unaffected. This behavior protects the Java program against unintended side effects. Chapter 6 - Methods
Passing Objects To Methods To pass an object (such as an array) to a method, Java makes a copy of the reference to the object, and passes that copy to the method. The method can use the reference to modify the object being referred to. Thus, an object passed to a method can be changed, and unintended side effects can occur. Chapter 6 - Methods
Duration and Scope Every variable in Java is characterized by a duration and a scope. The duration of a variable is the time during which it exists. The scope of a variable is the portion of the program from which the variable can be addressed. Chapter 6 - Methods
Automatic Variables Variables defined within a method body are called local variables or automatic variables. These variables are automatically created when the method body is executed, and destroyed when execution ends. New variables are created each time that the method executes—no information is preserved between executions. These variables are said to have automatic duration. Chapter 6 - Methods
Scope The scope of a variable is the portion of the program from which it can be addressed. There are two possible scopes for Java variables: Class Scope - accessible anywhere within a class Block Scope - only accessible with the code block (such as a method body) in which they are defined. Automatic variables have block scope—they can only be addressed from within the method in which they are defined. Chapter 6 - Methods
Method Overloading Java allows multiple methods to be defined with the same name within a given class, as long as they have different calling parameters. When a method is called, the Java compiler automatically compares the calling parameters with all method definitions, and selects the one with the matching set of parameters. Chapter 6 - Methods
Method Overloading (2) Calls int version Calls double version Method square with int calling parameter Method square with double calling parameter Chapter 6 - Methods