modifier returntype methodName (parameter)
{
// body
}
public static int display (int a, String b)
{
//method body
}
Program for swapping two numbers without using third variable.
class SwapNumber
{
public static void main(String[] args)
{
int n1 = 30;
int n2 = 45;
int temp;
System.out.println("Before swapping, n1 = " + n1 + " and n2 = " + n2);
swapMethod(n1, n2);
}
public static void swapMethod(int n1, int n2)
{
n1 = n1 + n2;
n2 = n1 - n2;
n1 = n1 - n2;
System.out.println("After swapping, n1 = " + n1 + " and n2 = " + n2);
}
}
class classname
{
Static
{
//code
}
}
public class StaticMethod
{
static
{
System.out.println("First static block"); //static block
}
static void m1 ()
{
System.out.println("Static method");
}
void m2()
{
System.out.println("Non-static method");
}
Static
{
System.out.println("Second static block"); //static block
}
public static void main(String args[])
{
m1(); //invoked static method
StaticMethod sm = new StaticMethod();
sm.m2(); // invoked non static method
}
}
protected void finalize ()
{
// finalize code
}