May 1, 2022

File Name Same as Class Name in Java

If you are reading the beginning chapter of any Java book or seeing any first Java program example online, one thing you will notice is that file name should be same as the class name in Java. In this post we’ll see how true is the statement that the file name should be same as class name in Java.

File name as class name in Java – Public class

If you have a public class in your Java program then it is mandatory that file name also should be same as the public class otherwise you will get error at the time of compiling the Java program.

For example, if you have the following public class.

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

You save this class as Test.java and try to compile it, you will get the following error.

D:\knpcode>javac Test.java
Test.java:1: error: class A is public, should be declared in a file named A.java
public class A {
       ^
1 error

Save the file as A.java and there is no error.

D:\knpcode>javac A.java

D:\knpcode>java A

Hello World

So you see the statement file name should be same as class name in Java is true if the class is having the access modifier as public.

File name as class name in Java– Class with default access

Now let’s see what happens if you have class with default access.

Consider the following program-

class A {
  public static void main(String[] args) {
    B objb = new B();
    objb.display();
  }
}

class B {
  public void display(){
    System.out.println("Hello World");
  }
}

Which is saved as Test.java, on compiling it there is no error.

D:\knpcode>javac Test.java

So you see with default access class, the statement file name should be same as class name in Java doesn’t hold true.

But notice the generated .class files after compilation of Test.java. Since there are two classes in the file so two .class files will be generated. You can see the .class files have the same name as the class name.

  • A.class
  • B.class

What that means is running the Java program still requires giving the same name as class name.

D:\knpcode>java A

Hello World

What we have seen is- If you have no public class then it is not required that file name should be same as class name, you can save the file with any name. But the .class files will have the same name as the default access class’ name.

Now you may ask why .class files have the same name as class name in this case, I can think of two reasons.

  1. As you must be knowing a Java file can have any number of classes with default access, only restriction is that there should be a single public class. If you see the example program, there also there are two classes. When you compile it separate .class files will be generated for the classes with in the file. Now the problem is, if you give the file any random name what would be the name of .class files (if there are more than one). That’s one reason that .class file has the same name as class name.
  2. Consider that you save this class as "Test.java" and let's assume some mechanism is used that all the other classes in the same file are also saved as Test1.class, Test2.class, ...

    If you see the example program, in class A object of class B is created using the statement.

    B objb = new B();
    

    Once compiler see this statement it knows it has to load B.class but as per our assumption it is saved as “Test1.class”. In that case Java compiler will have to maintain some mapping also to understand that B.class actually means Test1.class.

    That will result in a lots of overhead in the form of maintaining mapping, that means extra space and consulting that mapping to get the correct class meaning extra processing and all this for what? So we can give the file different name from the class!

So the end result is better to follow the convention that file name should be same as class name in Java whether it is a public class or class with default access.

That's all for the topic File Name Same as Class Name in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment