METHOD IN JAVA
A method in java is a set of statement which can be used to
perform a specific operation. The methods are known as functions
procedures, modules, subroutine or sub programs. There are two
types of methods
1. System defined methods
2. User defined method
SYSTEM DEFINED METHODS
The methods defined by the developers of the Java are called
system define methods. They are also known as library methods or
built in methods
Example [Link]()
USER DEFINED METHODS
The methods defined by the user are called user define methods.
These methods are defined as per need of the user to create
customized and reusable block of codes.
ADVANTAGES OF METHODS
▪ Methods can be reused in a program without the need to write
at the same code.
▪ To avoid duplication of the code and help in code reusability.
▪ It is divides complex programs in the manageable code blocks.
▪ It helps in understanding the flow of a program.
▪ It helps in improving readability of code.
▪ It make debugging of the code easier.
METHOD DEFINITION
Methods are defined inside the class immediately after the member
variables.
SYNTAX
Access specifier/ modifier return type method name ( parameter
list )
{
Method body
}
The method definition can be divided into 5
▪ Method access specifier
▪ Return type
▪ Method name
▪ Parameter
▪ Method body
ACCESS SPECIFIER OR ACCESS MODIFIER
The method prototype may begin with the access modifiers which
can be public, private, protected. These modifiers determine the
type of access to the method. They are called as access modifier or
access specifier.
RETURN TYPE
The return type of a method can be any valid Java data type. If the
method does not return a value then it must return a void type.
METHOD NAME
Method name can be of any valid Java identifier. We should always
give a meaningful names to the methods.
PARAMETER LIST
Parameter list is a sequence of data type and identifier pair
separated by comma. The perimeter list is also called parameters or
arguments. If a method has no parameter then its the parameter
list will be empty, but we must use empty round brackets ().
METHOD BODY
The statement within the method of body describe the task to be
performed.
METHOD HEADER OR METHOD PROTOTYPE
The first line of method definition is called method header or
prototype
METHOD SIGNATURE
The method name along with the list of parameters used in the
method prototype is called method signature
METHOD INVOKING
The method does not actually get executed until it is involved. the
methods can be involved or called or executed by specifying its
name followed by parameters being sent enclosed in the round
brackets.
Eg
Public class cube
{
Public void findcube( int x)
{
int result;
result=x*x*x;
[Link](“Cube =”+result);
}
Public static void main(String args[])
{
int s;
s=4;
cube cb = new cube();
[Link](s);
}
}
ACTUAL PARAMETER FORMAL PARAMETER
ACTUAL PARAMETER
The parameter that appear in the method invocation are called
actual parameter
FORMAL PARAMETER
The parameters that appear in the method definition are called
formal parameter or dummy parameter
DIFFERENCES BETWEEN ACTUAL PARAMETER AND FORMAL
PARAMETER
ACTUAL PARAMETER
▪ The parameter of the method when the method is called.
▪ They are not preceded by data type.
FORMAL PARAMETER
▪ The parameters of method when the method is actually
defined.
▪ They are preceded by data type.
DIFFERENCES BETWEEN CALL BY VALUE AND CALL BY
REFERENCE
CALL BY VALUE
▪ In call by value the formal parameter is copies of data from
the actual parameter.
▪ Changes made to the formal parameter do not affect the
actual parameter.
▪ The actual parameters in the function the data is of primitive
data type.
CALL BY REFERENCE
▪ In call by reference formal parameter just refers to the actual
parameter that is the memory location of the actual
parameter.
▪ Changes made to the formal parameter affect the state of
actual parameter.
▪ The actual parameters for the function that is the objects of
given class type are passed as a parameter.
DIFFERENCE BETWEEN PURE METHOD AND IMPURE
METHODS
PURE METHODS [ACCESSOR METHOD]
▪ Pure methods take objects or primitive data types as
arguments but does not modify the objects.
▪ Pure methods does not have side effects.
IMPURE METHODS [MUTATOR METHOD]
▪ Impure methods change the state of received object.
▪ Impure methods have side effects.
In what situation does a method a written a value ?
For a method a written a value it should have a return type other
than void in its method prototype and it should return a value of the
corresponding type using the returns statement in the method
body.
DIFFERENCE BETWEEN STATIC AND NON STATIC METHOD
NON STATIC METHOD
▪ The method which are accessed via an instance of the class
are known as non static method
▪ The instance is required to invoke a non static method also
known as instance method
▪ It can be invoked using the object of class. To call the non
static method use syntax
Objectname. Non static Methodname();
STATIC METHOD
▪ The methods which can be accessed without an instance of the
class are known as static method
▪ The instance is not required to invoke static method.
▪ It can be invoked using the class name to call the static
method use the syntax
Class [Link] methodname();
METHOD OVERLOADING
The process of defining two or more methods with the same name
but different signature(Parameters) in a class is called method
overloading