How to define a method in java?
SYNTAX:
Access Access Return Method (parameters list)
Specifier modifier type name
private static Primitive Valid Input to the
public data type identifier method
protected
Example:
public static void main(String args[ ])
public void sum( )
public void sum(int x, int y)
public int sum(int x , int y)
public static void sum(int x, int y)
, USER DEFINED METHODS:
Prototype : The first line of the method definition is called as
prototype.
Signature : Parameters(count and datatype) in the method definition
is called as Signature.
1. Formal parameter – parameters in the method definition
2. Actual parameter – parameters in the method call.
Two ways of invoking(calling) functions are:
1. Pass by value.(calling a method by passing a value)
2. Pass by reference.(calling a method by passing an address)
Non static Methods:
Method with no parameter and no return type:
class Example1
{
int a,b,s;
public void sum()
{
s = a + b;
System.out.println("Sum : "+s);
}
public static void main(String args[])
{
Example1 e = new Example1();
e.sum();
}
}