facebook

java recursion method not called twice why?

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #352287 Reply

    amir55
    Participant

    dear all

    I have a very simple java recursion which is worth to look at

    the program runs but it gives same result even at different passed value ? !!

    public class Simple {

    public Simple() {
    }

    static int i = 0 ;
    static String word ;
    public static String read (String name) {
    i++ ;

    if (i < 3){
    word = name ;
    read (name) ;
    }
    System.out.println (“word “) ;
    return word ;
    }

    public static void main(String[] args) {
    String result1, result2 ;

    result1 = read (“amir”) ;
    System.out.println (result1) ;

    result2 = read (“shankar”) ;
    System.out.println (result2) ;
    // why word shankar is not printed how to get it printed along with amir

    System.out.println (result1) ;
    System.out.println (result2) ;

    }

    }

    #352288 Reply

    support-swapna
    Moderator

    Amir,

    I am afraid this is a development related query and you should cross post to development related forums for help.

    Before doing so, please check the tutorials to understand how the static keyword works when using it for a counter. In your case, please recheck the value of i to understand why the program is not printing the desired result.

    Here is a quick read.

    Hope it helps.

    Please let us know if you see any MyEclipse specific issues.

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: java recursion method not called twice why?

You must be logged in to post in the forum log in