Learn about Method Recursion in Java Programming

Java (11) - Method Recursion 740X296

Java (11) - Method Recursion 740X296

In the previous article, we had a look at methods, and how they represent blocks of code that gets executed as one unit. You saw how they may or may not accept arguments, and how they may or may not return output. A method can call another method inside it. Even more, a method can call itself. So, welcome to Method Recursion.

Building a Recursive Search Application

The most common use of recursion is when you want to search for a file in a directory, which in turn may contain other sub-directories. You want the method to search in the first directory level. If the file is found, the method returns. Otherwise, it calls itself for new iterations to search the other directories so that the same operation is repeated again and again:

[File: Recurser.java]

[File: MainClass.java]

Java Programming Course for Beginner From Scratch

Don’t get intimidated by the File object. We are studying the java.io namespace in detail later in this course. Just grasp the idea of recursion from this example. Let’s discuss it step by step:

  1. We define a method called search that accepts a filename, and a File object (a type that refers to a file or a directory). It returns an integer output, which is used to indicate whether or not the method succeeded in finding the file (0 or 1).
  2. Then we assign the output of the listFiles() method to a File array. The listFiles() is called from a File object to list all the files and directories inside it. It returns an array that we assign to our filelist
  3. Now we have an array of files and directories in place, let’s iterate over it. We use the for loop here to work on each File f object in the array. We are building two loops actually: the first will search through all the files in the list and, when finished, the second loop will iterate through the directories.
  4. In the first loop, we are checking to ensure that we are working with files only. We don’t want to match a directory name to the string we are searching for. So we test if the file name is the same as the search string, if it is, print a message indicating success and return 0 signaling success. Note that the return keyword will make the method skip the remaining code and return back to the caller (the Main() function in our case).
  5. The second loop is handling directories. If the item is a directory, the method is calling itself again, supplying the search string and the current directory in the array as the first and second arguments.
  6. Finally the method is returning 1. This code will never be reached unless the both loops have completed and the search string has not been found. Accordingly, the method is informing the caller that it failed to find the file.
  7. In the Main() method you instantiate the Recurser class, create a new File object that represents the root directory you want to search in (“F:\\” in our example, and we used a second backslash to escape the backslash. This is the way Java understands Windows paths), and then call the search method passing in your search term, and the directory object as its arguments.

Notice here’s how recursion was used to make the method search all directories under the root partition F:\, no matter how deep it gets.


In this article, you have been introduced to Method Recursion by building a recursive search application. In Recursion, a method can call itself, with a different parameter value.

In the next article, we are going to talk about Method Overloading. See you there.


Please enter your comment!
Please enter your name here