title: Can I change variable name inside a loop in Java
body: <p>I want to change the variable name with each iteration. Since the number of nodes created is dynamically changing.</p>
  
  <p>I tried using one dimensional array but its returning a null pointer. My code is as follow</p>
  
  <pre><code>    GenericTreeNode&lt;String&gt; **root1[]** = null;
      for(int i=0;i&lt;10;i++)
      {
          String str="child"+i;
          System.out.println(str);
  
          **root1[i]** =new GenericTreeNode&lt;String&gt;(str);
      }
  </code></pre>
  
  <p>I am using already built datastructure</p>
  
  <pre><code>    public class GenericTree&lt;T&gt; {
  
  private GenericTreeNode&lt;T&gt; root;
  
  public GenericTree() {
      super();
  }
  
  public GenericTreeNode&lt;T&gt; getRoot() {
      return this.root;
  }
  
  public void setRoot(GenericTreeNode&lt;T&gt; root) {
      this.root = root;
  }
  </code></pre>
  
  <p>Is there some other way in java or JSP to change the variable name dynamically inside the loop.</p>