title: Why do java source files require package declarations?
body: <p>I think I am failing to understand java package structure, it seemed redundant to me that java files have a package declaration within, and then are also required to be present in a directory that matches the package name.  For example, if I have a <code>MyClass.java</code> file:</p>
  
  <pre><code>package com.example;
  
  public class MyClass {
      public static void main(String[] args) {
          System.out.println("Hello, World");
      }
  }
  </code></pre>
  
  <p>Then I would be required to have this file located in <code>com/example</code>, relative to the base directory, and I would execute <code>java com.example.MyClass</code> from the base directory to run it.</p>
  
  <p>Why wouldn't the compiler be able to infer the package name by looking at the directory structure?  For example, if I compiled the file from the base directory <code>javac com\example\MyClass.java</code>, I am not understanding why the MyClass.java wouldn't implicity belong to the <code>com.example</code> package.</p>
  
  <p>I understand there is a default package, but it still seems that the package declaration in the source file is redundant information?</p>