20

Possible Duplicate:
Why filename in java should be same as class name?

I have one file named temp.java. I wrote the following code. Why does this work?

class demo //not public keyword and not same as filename
{
    public static void main(String []args)
    {
        System.out.println("this is Main method");
    }
}

Any why does this not work?

public class demo
{
    public static void main(String []args)
    {
        System.out.println("this is Main method");
    }
}
Community
  • 1
  • 1
yogesh patel
  • 375
  • 3
  • 9
  • 20
  • 9
    Because it's the *law*! – Hovercraft Full Of Eels May 04 '12 at 04:35
  • Please have a look at the following link http://wiki.answers.com/Q/Why_do_file_name_and_class_name_always_coincide_in_Java – Chetter Hummin May 04 '12 at 04:37
  • 2
    How is this question marked as duplicate without a link to the dup? Is this some legacy 2012 magic? – Zsw Sep 25 '15 at 21:20
  • It is a rule in the language. Other languages - e.g. Xtend - do not have such restriction. – snorbi Nov 13 '15 at 06:41
  • I would like to answer in a simple language, if a class is referred in other compilation units( files outside the package, files within the package), then compiler does two things: 1) It searches for that class in the classpath , but its the duty of a program to tell where in the classpath , that class exisits. 2) It searches for the file name matching with the class name in the current classpath, when it gets the required file, then it searches for the class name inside the file. If in any of the options, everythng goes fine, then compilation also goes fine. – Rishab Shinghal Nov 19 '17 at 12:02

1 Answers1

26

In your first example, your class is actually declared as "package private" (no modifiers), which means only classes within the same package can access it. In your second example, you have declared it as public.

This is a scenario where the compiler has met the JLS quite well.

The JLS states:

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.

What this means is, for scenario 1, that because you only have temp.java with package private class demo, it is not being referred to by code in any other compilation units of the package, therefore it will compile without issue.

Your second scenario has declared the class public - which means it is potentially accessible from code in other packages - so it has to conform to the standards that the class name equals the file name.

If you created another class in your first scenario (within the same package) and then tried to reference the class demo, you should get a compilation error.

Community
  • 1
  • 1
Deco
  • 3,261
  • 17
  • 25