tags:

views:

188

answers:

4

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?

alt text

alt text

+10  A: 

Because the language designers say so. It really is that simple. It's a convention and they force you to follow it.

Glen
This simplifies class loading.
DR
When Java itself creates a class (as in the figure 2), it names it differently. I mean the class name and the file name that contains that class are different. Look closely at figure 2. You will see on the right-hand side that the file name is 'Book_1.java' while the class name is 'Book'. Do you see my point?
uzay95
@uzay95, Java doesn't create the class. Your tool/IDE does. I don't know what the tool is, but it looks like a bug. That or you're trying to create too Book classes in the same package.
Glen
It is netbeans 6.7.1 and it offered me to create a class name with Book even if it exists in the package.
uzay95
+4  A: 

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.

skaffman
+1 - It is definitely a convention, not a language restriction.
Stephen C
It's still a convention enforced by nearly all compilers and tools. Which effectively makes it a language restriction, even if not dictated by the JLS.
Joey
See Pascal Thivent's answer below; there **is** reference to this in the JLS.
delfuego
Aye, right enough, there it is. I just didn't look hard enough.
skaffman
+1  A: 

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
{

}

:-)

Sergey Mirvoda
But sometimes these rules makes more readable.
uzay95
More readable? for whom? When I open project I should be able _very_ easy find file by classname and find class by filename, and if tools force people to follow this rules I vote for it.
Sergey Mirvoda
+5  A: 

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 .java or .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.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in 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.

Pascal Thivent
Thank you for being the only person in this thread who found the relevant part of the language specification and quoted it, rather than talking out of your posterior. :)
delfuego
Regardless, the JLS says "may choose to enforce*, so it's still a convention, rather than a hard rule.
skaffman