-1

For example I have a file name call "A.java". Here its structure:

public class A{
}

private class B{
   // no error
}
public class C{
   //error : must declare at different file
}

I think this is Java rules. But, I really want to know: WHY java need to do that.

Thanks :)

Charles
  • 50,943
  • 13
  • 104
  • 142
hqt
  • 29,632
  • 51
  • 171
  • 250

5 Answers5

3

You can find in section 7.6 of the JLS:

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.

CsaByte
  • 724
  • 5
  • 7
1

There can only be one public class per .java file, as public classes must have the same name as the source file.

From your example: if you have file "A.java", only class A can be public.

Andriy Budzinskyy
  • 1,971
  • 22
  • 28
1

It is defined in the Java Language Specification.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

well,

you cannot have structure is a file called 'A.java' because you intended public class is named 'ClassA', then the file name must be ClassA.java. Now, as far as I understand, the idea behind this restriction is just to be organized. So you can have the physical origin of every class.

Without this restriction u can have a for example 'ClassC' coming from 'nowhere', and it could be really difficult to track the source code.

regards

fGo
  • 1,146
  • 5
  • 11
  • I think there were spaces missing in the question (I've edited it to add them). `public classA` -> `public class A`. – Bruno Mar 26 '12 at 17:00
0
public class A{
}

private class B{
   // no error
}

This shouldn't even work (see Java Language Specification):

It is a compile-time error if a top level type declaration contains any one of the following access modifiers: protected, private, or static

Are you sure you haven't declared class B as an inner class?

It doesn't make sens to declare a top-level class as private, since private restricts the visibility of members to the enclosing top-level class/interface. If your class is a top-level class (whether or not in its own file), you'd at least want package-level visibility (no modifier at all).

Bruno
  • 119,590
  • 31
  • 270
  • 376
  • Works for me! You can have only one *public* class and it must have the same name as the file, but as many private and default (package-level) classes of any name as you like in the same file. – Bohemian Mar 26 '12 at 16:41
  • @Bohemian, which compiler are you using? It doesn't work with OpenJDK 6's `javac`. (Default/package-level works, but that's not forbidden by the spec as far as I can tell.) – Bruno Mar 26 '12 at 16:42
  • @Bruno No. It works. Just not true for public. I'm using JDK 7. But I don't think it depend on Sun or OpenSource – hqt Mar 26 '12 at 16:55
  • @hqt, I'm merely quoting the spec. Doesn't work for me with Oracle JDK 7's `javac` or with Eclipse compiler either. As I said, private for a top-level class doesn't make sense anyway – Bruno Mar 26 '12 at 17:10