Java Methods
Java Methods
What Is Method?
A method is a collection of statements that are
grouped together to perform a specific Task.
Advantages Of Method
Write a method once and reuse it anywhere
Information hiding. Hide the implementation from the
user.
Reduce complexity.
Types Of Method
Predefined Method: are Thos method that are built in
Java like Math. abs()
User Defined Method: are Thos Method that are
defined by user
return expression;
Simple Method
class MyClass
{
Public
int
method
name
return
type
Modifier
parameter list
Method Continue
Modifiers:. This defines the access type of the method.
Return Type: A method may return a value. The returnValue
Type is the data type of the value the method
returns. Some methods perform the desired operations without
returning a value. In this case, the return Value Type is the
keyword void.
Method Name: This is the actual name of the method.
Parameters: A parameter is like a placeholder. When a method
is Call, you pass a value to the parameter.
Parameters are optional; that is, a method may contain no or
may not
Method Body: The method body contains a collection of
statements that define what the method does.
Static Method
When we use static keyword with method
Declaration is called static method.
Static method is called without class
object.
Static method
Memory is allocated multiple time whenever Memory is allocated only once at the time of
method is calling.
class loading.
Method Overloading
The Process of Declaring Multiple method
with same name but different parameter
is called Method Overloading
Whenever same method name is exiting
multiple times in the same class with
different number of parameter or different
order of parameters or different types of
parameters is known as method
overloading
Method overloading
class Addition {
void sum(int a, int b)
{
System.out.println(a+b);
}
void sum(int a, int b, int c)
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Addition obj=new Addition();
obj.sum(10, 20);
obj.sum(10, 20, 30);
}
}
Java Methods
The End