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
General Syntax of Declaring A Method
Modifier-return-value-type method-name( parameter1, parameter2 )
{
declarations and statements
}
Method can also return values:
return expression;
Simple Method
class MyClass
{
Public
int
min ( int num1, int num2 )
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.
Example of Method with Return type
public class Demo {
public int sum(int a,int b)
{
int c=a+b;
return c;
}
public static void main(String[] args) {
Demo x=new Demo();
int result=x.sum(10,10);
System.out.print("Sum="+result);
}
}
Example of method with non return type
public class Demo {
public void sum()
{
int a=10;
int b=20;
int c=a+b;
System.out.println("Sume="+c);
}
public static void main(String[] args) {
Demo x=new Demo();
x.sum();
}
}
Static Method
When we use static keyword with method
Declaration is called static method.
Static method is called without class
object.
Different between Static and Non Static Method
Non-Static method
Static method
These method never be preceded by static
keyword
Example: void fun1() { ...... ...... }
These method always preceded by static
keyword
Example: static void fun2() { ...... ...... }
Memory is allocated multiple time whenever Memory is allocated only once at the time of
method is calling.
class loading.
These methods always access with object
reference
Syntax: Objref.methodname();
These property always access with class
reference
Syntax: className.methodname();
If any method wants to be execute multiple
time that can be declare as non static.
If any method wants to be execute only once
in the program that can be declare as static .
Static Method Example
class A
{
void fun1()
{
System.out.println("Hello I am Non-Static");
}
static void fun2()
{
System.out.println("Hello I am Static");
}}
class Person{
public static void main(String args[])
{
A oa=new A(); oa.fun1(); // non static method
A.fun2(); // static method
}
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