Function
Static Method
public class Fun
{
	private static int num = 0;
	private int index;
	public Fun(int i)
	{
		num++;
		index = i;
	}

	int getIndex()
	{
		//instance method call static method
		System.out.println(getNum());
		//instance method access static attributes
		System.out.println(num);
		return index;
	}

	public static int getNum()
	{
		return num;
	}

	public static void main(String args[])
	{
		// create two objects
		Fun f1 = new Fun(1);
		Fun f2 = new Fun(2);

		// call instance method
		System.out.println(f1.getIndex());

		// call static method
		System.out.println(f1.getNum());
		System.out.println(Fun.getNum());
		System.out.println(getNum());
	}
}
			

Mutable and Immutable
public class Fun
{
	public static void changeStr(String s)
	{
		System.out.println("----------------");
		System.out.println(System.identityHashCode(s));//201869954
		s = "temp";//changing value creates a new object
		System.out.println(System.identityHashCode(s));//1311053135
		System.out.println("-----------------");
	}

	public static void main(String args[])
	{
		String s = "Hello World!";//201869954
		System.out.println(System.identityHashCode(s));

		changeStr(s);//not change s

		System.out.println(s);
	}
}
			
public class Car
{
	private String maker;

	public Car(String s)
	{
		maker = s;
	}

	public String getMaker() {return maker;}

	public void setMaker(String m) {maker = m;}

	public String toString()
	{
		return "Car: "+maker;
	}
}
			
public class Fun
{
	public static void changeCar(Car c)
	{
		System.out.println("----------------");
		System.out.println(System.identityHashCode(c));//2018699554
		c.setMaker("Honda");//changing value does not create a new object
		System.out.println(System.identityHashCode(c));//2018699554
		System.out.println("-----------------");
	}

	public static void main(String args[])
	{
		Car c = new Car("Buick");//2018699554
		System.out.println(System.identityHashCode(c));

		changeCar(c);//changed c

		System.out.println(c);
	}
}
			
Scope
Overloading
public class Fun
{
	public static int square(int n)
	{
		return n*n;
	}

	public static double square(double n)
	{
		return n*n;
	}

	public static void main(String args[])
	{
		System.out.println(square(10));
		System.out.println(square(3.14));
	}
}
			
Variable number of parameters
public class Fun
{
	public static void getStr(String ... args)
	{
		for(String s : args)
			System.out.println(s);
	}

	public static void main(String args[])
	{
		getStr("Hello", "World", "!");
	}
}
			
Recursive
//factorial
public class Fun
{
	public static int factorial(int n)
	{
		if(n <= 1)
			return 1;
		return n*factorial(n-1);
	}

	public static void main(String args[])
	{
		System.out.println(factorial(10));
	}
}
			
//fibonacci
public class Fun
{
	public static int fib(int n)
	{
		if(n == 0 || n == 1)
			return n;
		return fib(n-1)+fib(n-2);
	}

	public static void main(String args[])
	{
		System.out.println(fib(10));
	}
}
			
//Hanoi
public class Fun
{
	public static void Hanoi(int n, int source, int temp, int dest)
	{
		if(n == 1)
		{
			System.out.printf("%d --> %d\n", source, dest);
			return;
		}
		Hanoi(n-1, source, dest, temp);//move n-1 disk from 1 to 2, using 3 as temporary holding area
		System.out.printf("%d --> %d\n", source, dest);//move the last disk from 1 to 3
		Hanoi(n-1, temp, source, dest);//move n-1 disk from 2 to 3, using 1 as temporary holding area
	}

	public static void main(String args[])
	{
		Hanoi(3, 1, 2, 3);
	}
}
			
Reference