tags:

views:

41

answers:

4

Guys, I've come across such legal behaviour:

File B.java:

final class C {}
final class D {}

File A.java:

class B {}
public class A {}

Questions:

  1. When class X is required to be placed into its own X.java file? Does class visibility/final matter here?
  2. Is there any official spec on this class/java relation?

Thanks a lot.

+1  A: 

A public class ClassName must be in a file called ClassName.java.

Non-public classes have no such restriction.

A consequence of this is that a Java source file can have only one public class but as many non-public classes as you wish.

cletus
+4  A: 

The de-facto standard in most implementations is that a source file can only contain one top-level public type definition. The name of the source file must be the name of that type.

A source file can contain nested types, but by definition they're not a top-level public type.

This is recommended in, but not required by, the Java Language Specification.

JLS 7.6 Top Level Type Declarations

When packages are stored in a file system, 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.

Note that final has nothing to do with accessibility, so it's not a relevant issue in this matter.

Related questions

See also

polygenelubricants
@polygenelubricants: Thanks for exhaustive answer :)
Max
+1  A: 

In Java, you can have only one top-level public class or interface in a source file, and the source file must have the same name as that class or interface.

This is not something that's in the Java language specification; this is an implementation-specific thing of Sun's (now Oracle's) implementation of the Java compiler. (Other implementations of the Java compiler might not require this).

Jesper
It is implementation-specific, but JLS does have a few things to say: implementations "may" choose to do this when packages are hosted in file systems, but in fact "must not" do this if they're stored in a database. So it's "in" the JLS (putting aside the issue of whether such non-mandatory non-specification belongs in there in the first place).
polygenelubricants
Ok... strange that the JLS has the "must not" requirement when stored in a database. What's the reason for that requirement?
Jesper
I don't know why the JLS has the "must not" requirement when stored in a database. But when packages are hosted in a database, what is a "source file"?
emory
+1  A: 

The Sun/Oracle compiler allows at most one public top-level class per file, in which case the file name must be the class name.

The Java Language Specification does not mandate this behaviour, but explicitly allows it:

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;

Michael Borgwardt