Java compilation of a .java file without a public class
<p>Okay, so a java source file <em>must</em> have at least one public class and the file should be called "class-name.java". Fair enough.</p>

<p>Hence, if I have a class, then the following would compile:</p>

<pre><code>public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
</code></pre>

<p>But what bugs me is that if I remove the 'public' access modifier from the above code, the code still compiles. I just don't get it. Removing it, the code looks like:</p>

<pre><code>class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!"); // Display the string.
    }
}
</code></pre>

<p>In the above code, since I removed the 'public' access modifier, my class has default or package private access, i.e. it can't be access from the outside world, only from within the package. </p>

<p>So my question is, how does the above code compile ? The file HelloWorld.java in this case does not have a 'public' HelloWorld class (only a package-private HelloWorld.class) and thus to my understanding should not compile.</p>

<p>Appreciate your time in answering my questions!</p>

<p>regards,
anon</p>
