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");
}
}
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 .
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With