title: Summing File Sizes in a Tree Modeling Files and Directories
body: <p>I have two classes that are supposed to represent files and directories (like the files and directories in a computer). The <code>File</code> class holds information about a file such as name and size (which is accessed here with <code>getSize()</code>) and the <code>Directory</code> class has two lists holding sub-files and sub-directories.</p>
  <p>Basically, I'm trying to recursively go through and add up the sizes of all the files in a given <code>Directory</code> (and it's sub-directories). Here is my code so far:</p>
  <pre><code>public int size(Directory localDirect){
      int sum = 0;
      for (File file : this.files){
          sum += file.getSize();
      }
      for (Directory directory : localDirect.directories){
          sum += size(directory);
      }
      return sum;
  }
  </code></pre>
  <p>When I run this I get a 0 every time irrelevent of the actual file size. My first guess was that since I set <code>sum</code> to 0 each method call it is just being reset to 0 each time. However, when I manually follow the logic through I can't figure out why that would reset <code>sum</code> since it's set to 0 before the method starts adding the sizes.</p>
  <p>Can anyone see what I'm missing or how this could be changed in general to work better?</p>