This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Same name of file and public class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Same name of file and public class" Watch "Same name of file and public class" New topic
Author

Same name of file and public class

Kejal Shah
Ranch Hand

Joined: Jun 27, 2003
Posts: 87
Hi all,

Y is it necessary to have the name of a public class and the name of the file containing this class to be the same?

Tx in advance


Kejal<br />SaneDevil@gmail.com
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Why is the sky blue?
Why is water wet?

Some things just ARE. Someone made a conscious decision and put it into law. That law is enforced by the compiler.

You could as easily ask why you call an integer an int and an object an object.


42
Kejal Shah
Ranch Hand

Joined: Jun 27, 2003
Posts: 87
Thanks for your reply. But, i do believ that this has s/t to do with the public class being accessible from anywhere.

any takes?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
That is an implementation detail of the JVM which can use the law to more easily find the class it needs.
Other mechanisms can be devised as well to find which file contains which class.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
From JLS Section 7.6:
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).


For SCJP you have to know that a public class, A say, must be defined in a file named A.java. You do not have to know why.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
Another interesting thing is that an empty java file with no content does not give any compilation error. But it may not generate any class files (I hope)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
I think the entire file is silently skipped...
A sourcefile may contain AT MOST one public class which then must have the same name as the file.
It may contain ANY number of non-public classes.
Combining the two, a source file containing no classes is therefore legal.
As classfiles are created one per class, no classfile would result if there were no classes defined in a source.
Anil Hulikal
Greenhorn

Joined: Apr 02, 2004
Posts: 29
I do not think it is a good idea to snub anyone, however simple and trivial the question, that the person is asking, might appear on the surface. Perhaps, this is the reason why we have more programmers than software engineers today!. I encourage you to read this interview.

http://java.sun.com/developer/technicalArticles/Interviews/livschitz_qa.html


Just stay focused.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Same name of file and public class
 
Similar Threads
Class Declarations
Interface
What will be the name of .java file?
A very simple question (Please Answer)
public class filename????