Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:58:22+00:00 2026-06-03T19:58:22+00:00

I want to use an index variable inside a recursion, without sending it as

  • 0

I want to use an index variable inside a recursion, without sending it as a parameter when calling the function. However, if I reset it at the beginning (e.g i = 0), it will reset on every run. I want to use it as a counter (to count the function runs).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:58:23+00:00Added an answer on June 3, 2026 at 7:58 pm

    First of all, you will obviously want to initialize it only once. A common pattern in recursion is:

    public void run(int opt) {
      run_helper(opt, 0);
    }
    
    private void run(int opt, int depth) {
      if (whatever) { run(opt, depth + 1); }
    }
    

    Where the outer method does nothing but some initialization.

    A “solution” you will see suggested often (e.g. in the first comment to your question) will be to use static variables. This approach is a bad style, and will cause your program to fail in various weird way once you add multi-threading (e.g. by making a UI version, or run it in multithreading web server). And the worst is that it may at first appear to work, and only start misbehaving subtly when there are many users. So keep away from “static” for everything except constants!

    With “static” it would commonly look like this:

    static int counter;
    
    public void start() {
      counter = 0;
      recurse();
    }
    
    public void recurse() {
      counter += 1;
      if (whatever) { recurse(); }
    }
    

    Now imagine that two users invoke start at the same time. They will overwrite each others counter! Because static means, it’s shared amongst threads and users.

    Here is a really simple to understand solution:

    class MyTask {
      int counter = 0;
    
      public void recurse() {
        counter++;
        if (whatever) { recurse(); }
      }
    
      public int getIterations() {
        return counter;
      }
    }
    
    public void run() {
      MyTask task = new MyTask();
      task.run();
      System.out.println("Task took "+task.getIterations()+" itertions.");
    }
    

    You then create a task, run it, and retrieve the counter at the end. Clean, dead simple, efficient and reliable. If you have more than one thread/user, each will have a separate MyTask object, and you won’t run into any problem.

    Plus, you can add additional statistics, and they are all cleanly wrapped in the task object. “Average time per iteration”? “Average recursion depth”? No problem. The task object can also be used to store your result.

    The use of ThreadLocal has been suggested here. I do not agree with this. It offers no benefits of the task object at all. Just try to implement it with ThreadLocal and the task object and you’ll see the difference. Plus, ThreadLocal is empirically 10x slower than accessing heap values (see https://stackoverflow.com/a/4756605/1060350 ). For int it likely is even worse. So never ever call ThreadLocal#get in a performance critical codepath. If you intend to use ThreadLocal, use it outside of this codepath, and use a local variable (or a Task object) to feed the “local static” variable into your critical codepath.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use a variable as the index parameter in an array but
I want to use the value of a variable as an index of an
I want to use SOLR's remote-streaming facility to extract and index the content of
I want to use designated initializers, but what about the others index that aren't
I want to use ADO.NET Prepare command will increase performance if query is repeatevely
I want to know how to use PHP session variable in zend framework here
I want to use a URL like this: site.com/car-list/param1/value1/param2/value2 car is variable and can
Hi how i can pass variable into this function var _index = $(#sideNewsContent_menu li).index($(this))
I want to use values of the calling object within the jquery code block,
I want to use the jQuery :eq(index) selector dynamically, which means I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.