[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

calling same method from the same method body

Posted on 2014-03-27
3
Medium Priority
?
350 Views
Last Modified: 2014-04-29
Hi,

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

public Class XYZ{

public void methodA(){
....
...
methodA()
.......
}
}

When do we use this kind of scenario.

please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 668 total points
ID: 39959817
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:

http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/recursion2.html
http://www.sparknotes.com/cs/recursion/whatisrecursion/section1.rhtml
http://introcs.cs.princeton.edu/java/23recursion/

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.
0
 
LVL 28

Accepted Solution

by:
dpearson earned 664 total points
ID: 39960259
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,

Doug
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 668 total points
ID: 39964115
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 {
processFiles(file);
}
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question