title: different of defining object inside and outside loops in Java
body: <p>I was struggling with part of my code today to read some data from a file and add them to an object as its properties (I'm aware of how to add/read object in file without this hassle but wanted to do it this way) as below:</p>
  
  <p>file is like this:</p>
  
  <pre><code>111,john,23.1
  222,jack,22.5
  234,adam,12.8
  </code></pre>
  
  <p>I was trying to read this file using following:</p>
  
  <pre><code>public ArrayList&lt;Staff&gt; LoadAllStaffs(){
      ArrayList&lt;Staff&gt; staffs = new ArrayList&lt;Staff&gt;();
      File file = new File(stafffile);
      Staff tmpstaff = new Staff();
      try {
          BufferedReader inputfile = new BufferedReader(new FileReader(stafffile));
          String tmp;
          while((tmp = inputfile.readLine()) != null){
              StringTokenizer st = new StringTokenizer(tmp , ",");
              tmpstaff.setID(Integer.valueOf(st.nextToken()));
              tmpstaff.setFirstName(st.nextToken());
              tmpstaff.setSalary(Double.valueOf(st.nextToken()));
              staffs.add(tmpstaff);
              }
          } 
      catch (IOException e) {
      }
      return staffs;
  }
  </code></pre>
  
  <p>using <code>println</code> this output shown from the returned <code>ArrayList</code>:</p>
  
  <pre><code>234,adam,12.8
  234,adam,12.8
  234,adam,12.8
  </code></pre>
  
  <p>I just moved the <code>Staff tmpstaff = new Staff();</code> inside the while loop and it shows what it should.</p>
  
  <p>why this is happening? I read -even here- that defining variables (well, here its an Object ) inside or outside loops doesn't make any difference.</p>