There is no real reason for it, and as far as I know it is not a requirement that is in the JVM specification or the Java language specification. It's just the way that Sun's implementation of Java chooses to organize things.
Once I read somewhere that originally it was implemented this way in the Oak language, which is the predecessor to Java. By requiring that the filename of the source file would be the same as the public class in the source file, some kind of optimization could be done in the compiler for Oak. Java inherited this feature from Oak.
This restriction, of having the file name same as the public class name, makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type code.wiz.Bank would be found in a file Bank.java in the directory code/wiz, and the corresponding object code would be found in the file Bank.class in the same directory.
Also you should know that it is not mandatory to say "file name equals to classname". You can give your own name to your filename [ other than classname ] at the time of compilation you just give your filename[other than classname] .After compilation you will get .class file with your class name.[classname.class] .But at the time of loading your program into JVM you just have to give the class name. This is possible even the main() is public/private.
[edit] We say this statement that the file name should be same as the class name to make sure there is no confusion while compiling and running the program. Consider you have created many programs in java and now you want to run any one of them ,then it would be very difficult for you to recall the class name of that particular program. So to make it a simpler we often say that the class name should be same as the file name. [edit]
Hope this makes things better
This message was edited 1 time. Last update was at by sudipto shekhar
If a file doesn't contain a class with a public accessor and if it contains one or more classes with the default accessor (package level access), then the filename of the file can be anything that you choose. This is true even if the file contains a class that has the public static void main(String [] args) method. For example, the class below is the sole class in the file StringTest.java
C:\TEMP>javac StringTest.java
C:\TEMP>java StringTest
10String7
C:\TEMP>
This message was edited 2 times. Last update was at by Harry Henriques
The Java ..\bin directory has to be in your PATH environment variable on a WINDOWS machine. The fully qualified PATH to the ..\bin directory is dependent on where in your harddrive directory hierarchy you installed Java.
Using the code exactly as listed in this thread, I was able to invoke the following commands:
C:\TEMP> javac StringTest.java
This command compiles the code.
C:\TEMP> java StringTest
This command invokes the compiled bytecode.
This example will not work if your PATH environment variable in WINDOWS is not configured correctly. REMEMBER: you have to reboot your computer after you configure the PATH environment variable. The new PATH doesn't become effective until your computer has been rebooted.
The CLASSPATH variable has to be defined in your environment variables. CLASSPATH should be equal to .; this is a dot followed by a semicolon.
reboot your computer after creating the CLASSPATH and modifying the PATH.
Regards, Harry
This message was edited 3 times. Last update was at by Harry Henriques