1
2
class School
{
public static void main(String args[ ])
{
int x = 7, y = 5, z = 4 ;
double a;
School ob = new School( ) ;
a = ob.avg(x, y, z) ; Method Calling or Invocation
System.out.println(" Average = " + a) ;
}
7 5 4 Call-by-Value
public double avg(int n1, int n2, int n3)
{
double rs ;
rs = (n1 + n2 + n3) / 3.0 ;
return rs ;
}
}}
x n1
y Actual Arguments n2 Formal Parameters
z n3
3
Access-Specifier Return-Type Method-Name
public double avg( int n1, int n2, int n3 )
{
……………. Parameter-List
……………. Body of the Function
…………….
}
Note : The data-type of each variable in the
parameter-list is to be specified individually.
4
class Box
{
public static void main(String args[ ])
{
double L = 7.5, B = 5.0, H = 2.5 ;
double v;
Box ob = new Box( ) ;
v = ob.vol(L, B, H) ; Method Calling or Invocation
System.out.println(" Vol. of the box = " + v) ;
}
Call-by-Value
7.5 5.0 2.5
public double vol(double L1, double B1, double H1)
{
double rs ;
rs = L1 * B1 * H1 ;
return rs ;
}
}}
The Actual Arguments are : ………………………………..
The Formal Parameters are : ………………………………
5
6
7
8
import java.util.* ;
class School Optional
{
public long fact(int num)
{
long f = 1;
int j ;
for(j=num ; j > 1 ; j--)
{
f=f*j;
}
return f ;
}
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in) ;
int N ;
long rs ;
System.out.println("Enter the number : ") ;
N = sc.nextInt( ) ;
School ob = new School( ) ;
rs = ob.fact(num) ;
System.out.println("Factorial of the input number = " + rs) ;
}
}
Note : A function can be defined either above or below the
“public static void main(…)”
9
“static” Methods
A “static” is that which is defined using the “static” modifier
and can be invoked without an object.
Access-Specifier Modifier Return-Type Method-Name
public static double avg( int n1, int n2, int n3 )
{
……………. Parameter-List
……………. Body of the Function
…………….
}
10
class School
{
public static void main(String args[ ])
{
long p = 500000 ;
int t = 3 ;
double r = 7.1 ;
double I ;
i = SI ( p, t, r ) ;
System.out.println(" Simple Intt. = " + i ) ;
}
public static double SI ( long p1, int t1, double r1)
{
double rs ;
rs = p1 * t1 * r1 / 100 ;
return rs ;
}
}}
11
Method Signature and Method Header
Method Header
public static double avg( int n1, int n2, int n3 )
{
…………….
……………. Method Signature
…………….
}
Method signature is the combination of the
method name and the parameter list.
Q1> What is a method header in Java?
Q2> Differentiate between method header and method
signature.
12