whats the significance between having the java class containing the main method setup as public and not
<p>When I started learning Java I too started with the ubiqutous Hello world app. Which is more or less like :</p>

<pre><code>public class MyClass {

 public static void main(String args[]){

    //some code here....

 }

}
</code></pre>

<p>Its been many years since my tryst with Java but recently I was confronted by a beginner with a problem : Her class file wouldn't run even though she was able to compile correctly.</p>

<p>After looking at her code the first thing I noticed was that is didn't have the public access specifier on the class with the main method. I immediately mocked her on that slip up and went about "fixing" it by adding the public access modifier. The joke was on me offcourse as that didn't make any difference either. I then realized that she was trying to execute the compiled class from a different directory from the one where the class file was. I changed to that dir and issued the java command and it ran immediately. </p>

<p>This brings me to my question... Most hello world examples have the class setup with public access when its just as ok to not have that at all. So what difference does it make  if at all for a class that contains the main method ? </p>

<p>To help make it clearer as to what we were doing exactly here are the steps :</p>

<p>We had the class in </p>

<pre><code>c:\users\[usersname]\[my documents folder]\myprog.class
</code></pre>

<p>and we tried to run it from c:\ (as our working dir) . We did specify the full relative path expecting it to work. like so :</p>

<pre><code>java users\[usersname]\[my documents folder]\myprog
</code></pre>

<p>Offcourse this just failed untill I changed into the documents folder making it the current working dir and issuing the command again like so</p>

<pre><code>java myprog
</code></pre>

<p>So basically such a call makes sense when you have the class in a package resembling the folder structure prepended to its name. In this case the class is not in any package.</p>
