Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the class name and file name are same?

 
Prasath Thirumoorthy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rules are rules. Public classes must have the same name as the file they are defined in.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic