What is method recursion?

A recursion is nothing but, invoking a method itself. For example, in below code snippet, the method test() calls itself.

class Abc{
public void test()
{
System.out.println("test() method");
test();
}
}

However, do not try to use above code snippet, as this will continuously gets into recursion. Programs having such endless recursion, runs until java.lang.StackOverFlowError, is being thrown. This Exception is Unchecked Exception, and is thrown when stack gets full. The reason stack gets full is, an entry is added to stack whenever a method is invoked.Continuing to add entries to stack results in overflow of stack, and java.lang.StackOverFlowError exception is thrown.

Can static method call itself recursively?

Yes, static methods can call itself recursively.

How to use method recursion, effectively?

Remember that whenever a method is called recursively, there need to be condition, which need to control it. And there need to arise a situation where this condition is satisfied.Below is program, for fibonacci, using recursion.

public class FibonacciRecursion {
	public static void main(String args[])
	{
		int i = 0, j =1;
		fib(i,j);
	}
	
	private static void fib(int x, int y)
	{
		if(x>=30)
		{
			return;
		}
		
		System.out.print(y+"  ");
		
		fib(y, x+y);
	}
}

Also note that, above recursive version is more compact compared to non recursive version of fibonacci program.

What is indirect recursion?

A method test1(), invoking test2(), test2() further invokes test1(). This is termed as indirect recursion. In the example taken there are only two methods involved, also remember that there can be more methods involved, in indirect recursion. Indirect recursion may also end up with java.lang.StackOverFlowError exception, if not used cautiously.

Below code snippet shows non recursive method, as display() invokes super.display(), and not itself.

class Abc{
public void display()
{
System.out.println("Abc.display()");
}
}

class Xyz extends Abc{
public void display()
{
super.display(); //invokes display() method of Base class
System.out.println("Xyz.display()");
}
}

You may also like to read:
When OutOfMemoryException is thrown?
Difference between checked and unchecked Exception?
Purpose of static keyword?
How to invoke Base class constructor?

purpose of static members

A member of a class may be delcared as static.

A static member, is associated with the class(in which it is delcared), and not with the indidvidual objects.
Since static members are associated with class, they can be directly accessed with class name.
static members can be accessed, even before creation of object of the class.

A data members declared as static has common storage and value for the class, i..e for all objects of the class.

From a non static method both static and non static data members and member methods can be used or invoked.
Where as from static method only static members can be used or invoked. So, to invoke non static methods, from a static method, we need to create object of class, whose method need to be invoked.

this keyword cannot be used in static methods, as both of them contradict. static is unrelated to object, where as this refers to current object.

static example

//Example of static keyword
class Abcd
{
	static public int i;
	//i value is common to all object of Abcd
	//static members are associated with class, and not with object

	int j;
	//each object of Abcd, can have different value of j


	public int getI()
	{
	return i;
	}

	public int getJ()
	{
	stTest();
	return j;
	}

	//static data members/methods can be used in non static methods
	//but a static method can refer only static methods or data members
	public static void stTest()
	{

	System.out.println("This is static method");
	//j = 10; //error, only static variables can be referenced
	}
}

public class StaticExample {

	float met1()
	{
	return 0.0f;
	}

	public static void main(String args[])
	{
	Abcd.i = 50;

	//Abcd.j = 20;
	//Compile error, j cannot be accessed directly with class name
	//because j is non static

	StaticExample se = new StaticExample();
	se.met1();

	Abcd.stTest();

	Abcd obj = new Abcd();
	//obj.j = 34;
	//obj.i = 20;

	System.out.println("Abcd.i is "+Abcd.i);
	System.out.println("obj.j is "+obj.j);
	}
}

You may also like to read:
this keyword
How to declare static block
Are static constructors possible?
purpose of static method in abstract class

static method in Abstract class

Can a static method be a member of Abstract Class?

Read here for What is a abstract method? and what is abstract class?

static method is associated with class and not with individual objects of the class.

Yes, an abstract class can have static methods. But a static method cannot be abstract.

Also remember that, static methods take part in inheritance.
But static methods do not take part in polymorphism.

Below example shows that an abstract class, can have static method.


abstract class Abs1{
	int i,j;
	Abs1(){
		super();
		System.out.println("Abs1() Constructor");
	}

	Abs1(int i, int j)
	{
		this();
		this.i = i;
		this.j = j;
		System.out.println("Abs1(int i, int j) Constructor");
	}

	public static void met1()
	{
		System.out.println("met1() method in Abs1 class");
	}
}

public class AbstractDemo extends Abs1{

	AbstractDemo(){
		System.out.println("AbstractDemo() Constructor");
	}
	public static void main(String args[])
	{
		AbstractDemo obj = new AbstractDemo();
		System.out.println("in main() method");

		Abs1.met1();
	}
}

You may also like to read:
Difference between static and non static block
how many keywords or reserved words in java
purpose of static inner class