When reading Java you will come across this statement that the java file name should be the same as a public class name.

In the following, you will see how true this statement is that file names should be the same as the class name.

When you write a java program first it is saved as a Java file and then the compiler compiles it in byte code that is also called a .class file.

When you make your java program file similar to the class file, you can understand it without any ambiguity. And you can use a different filename only then when the class is not public, otherwise, in the case of a public class, you cannot use a different name.

What happens is that when the filename and the public class name are similar it tells the JVM that this is an entry point.

And it is also handy for developers to locate the class name when the Java file name and public class name are the same. For example, if you have used different file names for a specific class, the compiler will have to look for the class in all files.

 On the other hand, if the filename is the same as that of the class, it will increase its performance in locating and using the class.

Moreover, when you create a java program that contains more than one class and the program compiles a java source file, it produces the same number of the .class file as classes residing in the program.

In this case, you cannot easily identify which class java interpreter will interpret and which class consists of the entry point for the program.

Example 1:

For example, if we have the following public class:

And if we save this file as Test.java and try to compile it, we will see the following error.

However, when we save the file as A.java, we will see no error.

Here Code:

public class A {
    public static void main(String[] args) {
        System.out.println("Hello World");

Example 2: (Triangle and Demo)

Now you see two classes Triangle and Demo, in the example. Let’s see which class name should be considered the file name?

Here Code:

class Triangle {
            int x, y;
            void printArea()
                        System.out.println("Area of triangle is: " + x * y / 2);
class Demo {
            public static void main(String args[])
                        Triangle t = new Triangle();
                        t.x = 10;
                        t.y = 13;

Here you should see both the classes Triangle and Demo and see which class name has the main method and which one doesn’t have it because execution starts from the main method.

This example clearly shows that the class name Demo has the main method, so it must be the file name. 


In short, when you have a similar filename and class name it tells the JVM that it is an entry point, which also increases its performance in locating and using the class. However, when the program you create contains more than one class you will not be able to identify which class the java interpreter will interpret and which class contains the entry point for the program.

Recommended Articles:

You cannot copy content of this page
Scroll Up

  • Name

  • Minimum length of 8 characters.