title: How to best store data when working with recursion, and reset the data if method is re-called
body: <p>I'm tasked with searching recursively and iteratively for a string in a given input. More specifically I need to ensure that all parentheses brackets have an opposite one to match it. My code returns true if paratheses are all matched and false if they aren't. My problem is with the recursion as I'm struggling to track the parentheses. My code beneath works perfectly when called once, but if I call it multiple times the integers cross-track and it causes out of bounds exceptions. I want to know the best way that I can store the values when calling the function, as well as how I can reset the integers when the function gets called again.</p>
  <p>In other words, how can I reset these numbers after calling the code so that it works when called again, and what's the best way to track a number within recursion?</p>
  <pre><code>public static int tempSpot = 0; // this integer stores the position of the string im at
                                  // since I'm not allowed to use a for loop to serve the same purpose
  public static int openCount1 = 0; // track the count of &quot;(&quot;
  public static int closeCount1 = 0; // track the count of &quot;)&quot;
  
      public static boolean balanceParenthesesRecursive(String str){
          if (str.charAt(tempSpot) == '(') {
              openCount1++;
          }
          if (str.charAt(tempSpot) == ')') {
              closeCount1++;
          }
          tempSpot++;
          if (tempSpot == str.length()) {  // prevents recursion if next call will be out of bounds
              return openCount1 == closeCount1;
          } else {
              balanceParenthesesRecursive(str);
          }
          return openCount1 == closeCount1;
      }
  </code></pre>