aspose file tools
The moose likes Beginning Java and the fly likes Why java file cannot consist two public classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why java file cannot consist two public classes" Watch "Why java file cannot consist two public classes" New topic
Author

Why java file cannot consist two public classes

davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Can anyone please tell me why a java file cannot consist two public calsses with detailed explanation.

Thanks in advace.

regards,
david
[ March 03, 2007: Message edited by: davidson john ]
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2343

It's your java platform that decides whether this is enforced or not.
But it's a convention among the java community to work this way for java platforms that use a file system to store the sources.

See the java 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. A compile-time error occurs if the name of a top level type appears as the name of any other top level class or interface type declared in the same package (�7.6).



The convention is documented in Code Conventions for the JavaTM Programming Language , 3 - File Organization.

Regards, Jan


OCUP UML fundamental
ITIL foundation
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 407
1.The execution of a file starts from the main() method of the class declared as public,so if two classes are declared public jvm cannot decide from where to start the execution.
2.compiler also doesn't allow two public classes in the same file.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24044
    
  13

Originally posted by Raj Kumar Bindal:
1.The execution of a file starts from the main() method of the class declared as public,so if two classes are declared public jvm cannot decide from where to start the execution.


Well, no. You don't execute source files, you execute class files. A class file, by definition, has only one class file in it.

The answer is just as Jan has given it above; it's a convenience for compiler implementors.


[Jess in Action][AskingGoodQuestions]
davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Thank You Jan Cumps & Ernest Friedman-Hill. Now i have a clear understanding of the reason behind having a single public class in a java source file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why java file cannot consist two public classes
 
Similar Threads
only one PUBLIC CLASS per source file..... But Why..?????
main() method...
Two classes with public access specifier in a single source file
The Source file
Top level class?