• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

calling same method from the same method body


Are there are scenarios  of calling same method from the same method body

public Class XYZ{

public void methodA(){

When do we use this kind of scenario.

please advise
Any links resources ideas highly appreciated. Thanks in advance
3 Solutions
käµfm³d 👽Commented:
This is called "recursion", and it solves a certain set of problems. I'm not certain how best to explain it in straight-forward terms, so I'll offer the following in the hopes that they do a better job explaining than I would:


You do need to be careful when defining recursive functions to make certain you can meet some exit condition, otherwise you will end up with endless recursion, which will ultimately overflow your stack.
A good example of when recursion is helpful is when the data structure you are working with has a repeating structure - like a binary tree.  Every node in the tree looks the same as every other node - with 2 child nodes beneath it.

Here's an example of how we might represent a binary tree in Java:

public class BinaryTree {
  public static class Node {
    private Node left; 
    private Node right;
    private int value;
    public Node(int value, Node left, Node right) {
      this.value = value;
      this.left = left ;
      this.right = right ;

   private Node m_Root ;

Open in new window

Now think about how you'd write a function that visits all of the nodes in the tree?  If you start writing a method to do that you will find it's quite hard.  You may want to try that as an exercise.

But we can do it with a recursive (calling itself) function quite easily:

public void printTree(Node node) {
    if (node == null)
        return ;

    System.out.println(node.value) ;
    printTree(node.left) ; 
    printTree(node.right) ;   

public void printEntireTree() {
     printTree(m_root) ;

Open in new window

Hope that helps explain why we might want functions like this,

Another example of how that can be frequently used is with file structures where you have a directory with subdirectories that, in turn, have subdirectories, etc. where the desire is to process the files in any an all of those directories. In pseudo code
processFolders(mainpath) {
file[] folder = [Array of names in mainpath]
for (int k = 0; k<folder.length;k++){
String fileOrFolder = folder[k];
File file = new File(fileOrFolder);
if (file.isDirectory() {
 processFolders(fileOrFolder); ==> calls itself
} else {
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now