aspose file tools*
The moose likes Beginning Java and the fly likes Why the class name and file name are same? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why the class name and file name are same?" Watch "Why the class name and file name are same?" New topic
Author

Why the class name and file name are same?

Prasath Thirumoorthy
Ranch Hand

Joined: Jul 23, 2004
Posts: 65
Hi Friends,

Why the class name and the file name are same in java ?.

Consider this,
*********************************************************
class Example {
public static void main(String arg[]){
System.out.println("Hello World"):
}
}
*********************************************************
This file is saved as TestExample.java.But the Class Name is Example.
This is compiled n run
Output: Hello World
Even though the class name n file name are different.


Consider this,
In this case the file saved as TestExample but its not compiling
bcos the class is declared as public

Its giving compilation error:
/**
TestExample.java:1: class Example is public, should be declared in a file named F
Example.java
public class Example {
^
1 error
*/

*********************************************************
public class Example {
public static void main(String arg[]){
System.out.println("Hello World"):
}
}
*********************************************************

Can anyone help me what is reason for this.

Thanks & Regards
Prasath


Thanks,
Prasath

SCJP1.4, SCWCD
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Rules are rules. Public classes must have the same name as the file they are defined in.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you're interested in a reason behind those rules: because it makes it easier to implement a Java compiler. Most programs consist of hundreds if not thousands of classes. If each class is defined in a file named after itself, it's easy for the compiler to find it. That's the only reason.


[Jess in Action][AskingGoodQuestions]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Is that actually specified this way, or is it just that all compilers happen to work this way? I seem to recall from the early days of Java that some compilers did not care about this - Symantec VisualCafe I think was one of them.


Ping & DNS - my free Android networking tools app
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This is from section 7.6 of the JLS, second edition:


When Java 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 Java compiler and 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 Java packages are stored in a database (�7.2.2), the host system need not enforce such restrictions.

    In practice, many Java 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.


    So it's a rule that a particular implementation is officially allowed to enforce and still comply with the JLS.
     
    wood burning stoves
     
    subject: Why the class name and file name are same?