In Java, it is possible for the functions to call themselves. A function is called ‘recursive’ if a statement within the body of a function calls the same function. Sometimes called ‘circular definition’, recursion is thus the process of defining something in terms of itself.
Example
public class Rec { public static void main(String[] args) { myMethod(5); // counter is 5 } static void myMethod(int counter) { // whenever counter is 0 function returns to calling block if (counter == 0) return; else { System.out.println(counter); // decrement counter myMethod(--counter); return; } } }
Output
5 4 3 2 1