When i want to create a java class it is generating automatically a file with the same name of class.
But when it generate a class, it can change the file name different than class name.. Am i missing something?


When i want to create a java class it is generating automatically a file with the same name of class.
But when it generate a class, it can change the file name different than class name.. Am i missing something?


Because the language designers say so. It really is that simple. It's a convention and they force you to follow it.
The language specification itself does not dictate this (I've just had a look, and can find no reference to it), but it's generally enforced by tools. It makes it considerably easier for tools' dependency management, since it knows where to look for class B if class A has a reference to it. The convention extends to the directory structure echoing the package structure, but again, this is just a convention.
If I can change the world I wish c# designers also do that. How much time can be saved from forcing guys to not create file classes.cs and put ALL code in it. Isn't it such as requirement of braces for If. Why language force me do that silly thing:
if (true)
{
}
instead of
if true
{
}
:-)
Quoting the section 7.6 Top Level Type Declarations from the Java Language Specification:
When packages are stored in a file system (§7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as
.javaor.jav) if either of the following is true:
- The type is referred to by code in other compilation units of the package in which the type is declared.
- The type is declared
public(and therefore is potentially accessible from code in other packages).This restriction implies that there must be at most one such type per compilation unit. This restriction 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
wet.sprocket.Toadwould be found in a fileToad.javain the directorywet/sprocket, and the corresponding object code would be found in the fileToad.classin the same directory.When packages are stored in a database (§7.2.2), the host system must not impose such restrictions. In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other compilation units.