Methods in Java
Methods in Java
Function Definition
User-defined function
Parameter Passing
To solve a certain task, especially if it is a complex one, we apply the method that ancient
Romans did “Divide and Conquer”. According to this principle, the problem we solve must be
divided into small sub-problems. Taken separately they are well defined and easy to be resolved
compared to the original problem. At the end by finding solutions for all the small problems we
solve the complex one.
Using the same analogy, whenever we write a program we aim to solve particular task. To
do it in an efficient and “easy-to-make” way we use the same mentioned above principle “divide
and conquer”. We separate the given task into smaller tasks, then develop solutions for them
and put them together into one program called subroutines. In some other programming
languages, subroutines can be named as routines, procedures or functions but in general these
are called method.
where those inside [] are optional and those inside <> are required.
Page 1 of 4
This figure shows the parts of a method
Parts
o Modifiers – these are keywords that gives additional information of the method.
Common modifier is access modifiers. Access modifier define access type in the
method. In java, there are four type of access specifiers.
public
protected
private
default
o Return type – the data type of the value returned by the method or void if does
not return a value.
o Method name – the identifier of the method. This is used to call (invoke) the
method. In naming methods, it is recommended to use verb or verb plus noun.
o Parameter list – comma separated list of the input parameters are defined,
preceded with their data type, within the enclosed parenthesis. If there is no
parameters, you must use empty parenthesis. Any data type can be used as
parameters and in multiple parameters, different data types can also be used.
o Method body – enclosed between braces. The code you need to be executed to
perform your intended operations
Method Signature/Specification – consists of the method name and parameter list
(number of parameters, type of the parameters and order of parameters). Return type
and exceptions are not considered as part of signature.
Local Variables – these are variables declared inside the body of the method.
o Variable Scope – the area of visibility of the variable.
Invoking a method – a.k.a calling a method is the process of execution of the method’s
code.
o Completes all the statements in the method.
o Reaches a return statement
o Throws an exception
Page 2 of 4
Formal Parameters – these are the parameters in the parameters list.
Argument – the values assigned to parameter when method is called/invoked.
max(10,20);
Return type – Method can be classified as function or routine. Function returns a value
based on the data type used. Routine or procedure does not return a value and uses void
keyword. Void means that the method do not give value.
o Function requires return keyword and the return value should be under the
datatype used in creating the method.
o Routine does not require return keyword
Practice Time!!!
Further research is encourage about the concepts of method in java to strengthen your
understanding.
Creating sample methods in actual programs is highly suggested
Review and research on the previous topics.
Apply previous programming problems discussed to methods.
Try to understand and define and real essence of using method in programming.
Research on how to incorporate method in java.
Research on how to call method in java.
Create simple method in java based the above discussions.
//end of module 5
Page 4 of 4