Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why must a Java file have the same name as its public class? [duplicate]

Tags:

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");
    }
}
like image 954
yogesh patel Avatar asked May 04 '12 04:05

yogesh patel


People also ask

Why the Java file name should be always the same as a public class name?

The filename must have the same name as the public class name in that file, which is the way to tell the JVM that this is an entry point. Suppose when we create a program in which more than one class resides and after compiling a java source file, it will generate the same number of the .

Why a single Java source file can not have more than one public class?

So the reason behind keeping one public class per source file is to actually make the compilation process faster because it enables a more efficient lookup of source and compiled files during linking (import statements).

Is it mandatory to save the Java file with the same name as the class defined in the file?

It is not necessary to name your file same as the name of the class it has, until this class is public. Though it is a good practice to name the file same as the name of class. The compiler will compile your file successfully and make a dot class file.

Can 2 classes have same name in Java?

For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.


1 Answers

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.

like image 72
Deco Avatar answered Oct 01 '22 23:10

Deco