Java Methods
Java Methods
Methods
1
Introducing Methods
2
Method Definitions
• Methods belong to a class
• Defined inside the class
• Heading
• Method Return type (e.g. int, float, void)
• Method Name (e.g. nextInt, println)
• Method Parameters (e.g. println(…) )
• More…
• Body
• enclosed in braces {}.
• Declarations and/or statements.
3
Method Definitions
• Type specifies the type of data returned by the method.
• If the method does not return a value, its return type
must be void.
• The name of the method must be specified .
• The parameter-list is a sequence of type and identifier
pairs separated by commas.
• Parameters are essentially variables that receive the
value of the arguments passed to the method when it
is called.
• If the method has no parameters, then the parameter
list will be empty.
4
Method Definitions
• Methods that have a return type other than void return a value to the
calling routine using the following form of the return statement:
return value;
• Here, value is the value returned.
5
Adding Method to Box Program
6
Adding Method to Box Program
Output:
7
Adding Method to Box Program
mybox1.volume();
mybox2.volume();
• When mybox1.volume( ) is executed, the Java run-time system
transfers control to the code defined inside volume( ).
• After the statements inside volume( ) have executed, control is
returned to the calling routine, and execution resumes with the
line of code following the call.
• A method is Java’s way implementing subroutines.
8
Adding Method to Box Program
• There is something very important to notice inside the volume( ) method:
the instance variables width, height, and depth are referred to directly,
without preceding them with an object name or the dot operator.
• A method is always invoked relative to some object of its class. Once this
invocation has occurred, the object is known.
• Thus, within a method, there is no need to specify the object a second
time.
• This means that width, height, and depth inside volume( ) implicitly refer
to the copies of those variables found in the object that invokes volume(
).
9
Adding Method to Box Program
When an instance variable is accessed by code
that is not part of the class in which that instance
variable is defined, it must be done through an
object, by use of the dot operator.
However, when an instance variable is accessed by
code that is part of the same class as the instance
variable, that variable can be referred to directly.
10
Adding Method that return value
11
Adding Method that return value
12
Adding Method that return value
• There are two important things to understand about returning values:
13
Adding a Method That Takes Parameters
• Parameters allow a method to be generalized.
• That is, a parameterized method can operate on a variety of data
and/or be used in a number of slightly different situations.
• This method returns the value of 10 squared, its use is very limited.
14
Adding a Method That Takes Parameters
16
Adding a Method That Takes Parameters
17
Adding a Method That Takes Parameters
18
Example1 class Account {
int accno;
int balance;