title: Java: How to access a field that's being changed in a loop from another method?
body: <p>Here's the code:</p>
  
  <pre><code>public void play() {
          Action  action;
          Place   place   = getPlace(0);
          Item    item    = new Item("placeholder","a placeholder");   
          Scanner scanner = new Scanner(System.in);
          String  text;
  
          System.out.println(getDescription());
          while (null != place) {
              System.out.println(place.getDescription());
              place.printItemDescription();
              text    = scanner.nextLine();
              action  = getAction(text, place);
              if (text.startsWith("pick up")){
                  String obtain[] = text.split(" ");
                  label = obtain[2];
                  for (int i=0; i&lt;place.itemList.size(); i++){
                       if (label.equals(place.itemList.get(i).getLabel())){
                          itemList.add(place.itemList.get(i));
                          place.itemList.remove(place.itemList.get(i));
                      }else{System.out.println("There's no " + label + " in this place.");}
                  }
              }
              else if (text.startsWith("put down")){
                  String remove[] = text.split(" ");
                  label = remove[2]; 
                  for (int i=0; i&lt;itemList.size(); i++){
                      if (label.equals(itemList.get(i).getLabel())){
                          place.itemList.add(itemList.get(i));
                          itemList.remove(i);
                      }//else{System.out.println("Not a valid action");} Not yet working
                    }
              }
              else if (text.startsWith("inventory")){
                  System.out.println("Your inventory contains:");
                  for (int i=0; i&lt;itemList.size(); i++){
                     System.out.println(itemList.get(i).getLabel()); 
                  }
              } 
              else if (null != action) {
                  if (checkReq(action) &amp;&amp; checkForbidden(action)){
                      System.out.println(action.getDescription());
                      place   = action.getNext();
                  }
                  else if(!checkReq(action)){
                      System.out.println("You do not have the required item.");
                  }
                  else if(!checkForbidden(action)){
                      System.out.println("You have something that you cannot bring with you.");
                  }
              }
          }
      }
  </code></pre>
  
  <p>This is a snippet of a text game I'm working on. I have classes for Place, Action, and Item. I'm trying to create methods to put under the else and if else statement so instead of having: </p>
  
  <pre><code>  if (text.startsWith("pick up")){
                  String obtain[] = text.split(" ");
                  label = obtain[2];
                  for (int i=0; i&lt;place.itemList.size(); i++){
                       if (label.equals(place.itemList.get(i).getLabel())){
                          itemList.add(place.itemList.get(i));
                          place.itemList.remove(place.itemList.get(i));
                      }else{System.out.println("There's no " + label + " in this 
                        place");}
  </code></pre>
  
  <p>I would just have:</p>
  
  <pre><code>if (text.startsWith("pick up")){pickUp(text);}
  </code></pre>
  
  <p>where pickup is a method I write that contains all the code in that if statement above. I want to do this so the code is clearer, and I can unit test those methods instead of trying to unit test this play() method which is pretty bloated.</p>
  
  <p>The problem I'm having is the local variables. I know I can't access them in other methods. I was thinking that I could make them global variables, and while that would work for most, it doesn't work for place because place is going to change as the while loop runs. So I'm stuck and I was wondering if their was a way to do this or if I'm looking at this the wrong way.</p>