tags:

views:

379

answers:

4

There is a statement in the book I'm reading for the SCJP qualification, it says :

Files with no public classes have no naming restrictions

That has made me ask, why would you ever want to do this?

If there are no public classes, then how could other classes ever import and use the file? The only purpose I can see is if the file runs standalone in itself, which could also be odd, such as have an entire application in one file

+23  A: 

This is valid for package-private classes as well. And you can use package-private classes within the same package. (And in that case you don't have to import it, because it's in the same package.)

For example, the JapaneseImperialCalendar class is package-private, because it is only used from Calendar.createCalendar(..) - it is not part of the public API. You can't directly instantiate the japanese calendar, but you can still use it by its interface. Same goes for all unmodifiable collections that are obtained by methods like Collections.unmodifiableList(..) - they are package-private.

So the .java file of JapaneseImperialCalendar could've been arbitrary. However, it is advisable not to diverge from the established practice of naming even package-private files after the class name.

Bozho
As reinforcement of keeping the established naming practice just in case, note too that [the JLS states](http://java.sun.com/docs/books/jls/third_edition/html/packages.html#26783) that "the host system may...enforce the restriction that it is a compile-time error if a type is not found in a file [with that naming convention]" even in the case where the type is only "referred to by code in other compilation units of the package in which the type is declared" (package-private).
Tim Stone
+2  A: 

From Java Classes, you have public classes and package classes. Package classes are considered "private" so that you can only use them within the package itself. This is the default, i.e. no public is specified.

Public classes are, of course, classes that you can create anywhere.

progo
+2  A: 

You can create a file named package-info.java, which contains only a package statement. The javadoc 1.5+ tool treats a javadoc comment on this package statement exactly like a package.html file. In addition, you can add package-level annotations such as @Generated to this statement, which you can't do in package.html.

Because package-info is not a valid Java identifier, there is no risk of this file ever clashing with an existing Java class (i.e. backwards compatibility).

Barend
That's (probably) a different question you've answered here..
Tim
Actually, it's a very fitting example for why such a file would have no naming restrictions. The file name isn't even a legal class name, so it couldn't possibly be a file with a class in it (but it's still a *.java that the compiler is going to process according to the rules laid out in the JLS).
Barend
A: 

I don't agree with the non-restriction. Each java file should contain only one top level class, and the file name should be the same as the class name, public or not. I don't think javac would like this very much (or any human being)

A.java
    class B

B.java
    class A

http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.3

7.2 Host Support for Packages

Each host determines how packages, compilation units, and subpackages are created and stored, and which compilation units are observable (§7.3) in a particular compilation.

7.2.1 Storing Packages in a File System

As an extremely simple example,


http://download.oracle.com/javase/6/docs/technotes/tools/windows/javac.html

both source and class files must have root names that identify the class. For example, a class called MyClass would be written in a source file called MyClass.java and compiled into a bytecode class file called MyClass.class.

irreputable
It is allowed, like it or not. It's a different question whether this makes sense in the general case
Bozho
@bozho what was the original source of the file name convention? it's been a long while.
irreputable
JLS perhaps. I don't know how did it get there :)
Bozho
@bozho JSL wouldn't dictate such things.
irreputable