Method 2223
Method 2223
Definition
A Java method is a collection of statements that are
grouped together to perform an operation.
4.5 Method
Advantages of method
•Can breaking up programs and algorithms into smaller, more manageable pieces
•This makes it easier for writing, testing, and debugging.
•Easier to break up the work for team development
•It can be called to do their tasks anywhere in a program, as many times as needed
•Avoids repetition of code in a program .(can be placed into libraries to be used by
more than one "program“)
4.5 Method
Types of
Java
Method
a) b) User-
Predefined defined
Methods Methods
4.5 Method
4.5.2 Predefined method (T)
For example:
•print() is a method defined in java.io.PrintStream. The print("...") prints the string
inside quotation marks.
result = Math.pow(2,3);
System.out.println("The answer is: " + result);
Output:
} The answer is: 8.0
}
4.5 Method
Output:
Square root of 4 is: 2.0
4.5 Method
4.5.3 User-defined Methods
⮚ Before you can call or invoke a method, you need to define it based on the general structure of
methods containing:
• return type – the data type of the value returned by the method.
1. a method that returns no value should have a return type void.
2. the return type is based on the type of value returned by the method.
Eg: primitive data types (int, double, float, boolean, and char)
• method name - It is an identifier that is used to refer to the particular method in a program.
• parameters – a placeholder in the called method to hold the value of the passed arguments.
4.5 Method
b) User-define Methods
*** addition
Access Specifier: Access specifier or modifier is the access type of the method. It specifies the
visibility of the method.
Public The method is accessible by all classes when we use a public specifier in our application.
When we use a private access specifier, the method is accessible only in the classes in which
Private
it is defined.
When we use protected access specifier, the method is accessible within the same package
Protected
or subclasses in a different package.
When we do not use any access specifier in the method declaration, Java uses default access
Default
specifier by default. It is visible only from the same package
4.5 Method
b) User-define Methods – example 1
public class primary
{
public static void main (String [] args)
{
System.out.println("About to encounter a method.");
myMethod(); // method call
System.out.println("Method was executed successfully!");
}
{ Method definition
Method body
System.out.println("Printing from inside myMethod()!");
}
}
4.5 Method
b) User-define Methods – example 2
- _______________________________________________
•return type is based on the type •The name of the method is an •A parameter is a placeholder in
of value returned by the method identifier. the called method to hold value
•You can give any name to a of the passed arguments.
•Method can return value method. However, it is more
of primitive data types (int, conventional to name it after the •If there are values passed, then
double, boolean, and char). tasks it performs. the values will be assigned to the
parameters accordingly. (You can
•If the method does not return a For example: pass any number of arguments to
value, its return type is void. •CalculatePrice, ComputeMarks, a method.)
CalculateArea, and
FindMaximum. •However, methods may or may
not have parameters.
•usually represents the task
performed by the method
4.5 Method
Method Call
Method Definition
Method Definition General Format of Method Definitions
•A user-defined method must first be
defined before it is called. Method header
•Method definition consists of:
▪ Return type
▪ Method name Method body
▪ Parameters
▪ Method body
4.5 Method
Method Definition
This method:
•named DisplayMessage
•did not return any values, void and
Examples of method definition •did not accept any parameters, ().
}
4.5 Method
Method Call
variableName = methodName;
⮚ Printed using
System.out.print(methodName)
4.5 Method
Methods Call with no return value and no passing argument
Class AddMethod
{
public static void main (String[] args)
{
Addition(); // method call
}
}
4.5 Method
Check point 2