• Post Reply Bookmark Topic Watch Topic
  • New Topic

use of public with classes  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

i know about access modifiers etc,and how thy generally work
but how come you can do this:



and not this:



so how come you cant have two public classes in one file...was just wondering
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok just googled and found this:


It forces all Java code to be organized in a certain way, which in the long run helps improve code readability.

The Java designers chose a strict approach that enforces their idea of good design practices, and this is part of that theme. Contrast that with the anything-goes attitude in Perl.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rule is:

You can have only one public top-level class, interface, enum or annotation in one source file, and the source file must have the same name as the class, interface, enum or annotation.

There's really no strict technical reason why it needs to be like this, but it's a rule that the Oracle Java compiler enforces. And it indeed helps you to organize your source files a certain way, which will help you to have a consistent structure in your project which may contain many source files.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that actually a rule of the language? The only thing I could find about public classes in the Java® Language specification was this section, which doesn't address this particular problem.  Nor did I find much in the JVM specification or the javac tool manual (that is the Windows® version), but those of us more experienced all know about it. If your public class has a different name from the file name, most compilers won't compile it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java® Language Specification Java SE 8 Edition wrote:If and only if packages are stored in a file system (§7.2), 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 Java compiler to find a named class within a package. 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.

    Source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-7.html#jls-7.6 (emphasis mine).
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7987
    143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This actually means that the compiler may reject source files if they contain a type that has a different name than the file, if that type is referred to by other types in the same package.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:. . . Source: . . .
    So I was looking in the wrong place, or for the wrong search criteria. Thank you.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!