Thursday, September 9, 2021

Why Class Name and File Name Should be Same in Java

When we start learning Java and write our first "Hello World" program, there are two things that stand out.

  • File name and class name should be same in Java.
  • Main method signature- The main method signature must be public static void main(String[] args)

Refer Why main Method static in Java to understand the requirement why main method should be static.

Here we'll talk about the requirement that File name and class name should be same in Java. Let's be clear on one point; this requirement is not mandatory unless until there is a public class in the file.

If there is a class with access modifier as public in the file then it has to be saved with the same file name.

File name and class name same in Java - Public class

Let's see the case when we have a public class.

public class Test {
  public static void main(String[] args) {
    System.out.println("This is a test class");
  }
}

If we save it as Thetest.java then we'll get an error while trying to compile it.

class name and file name same in Java

It can be seen how compiler complains about having the public class Test and how it should be declared as Test.java.

Now if we save the class as Test.java then it compiles and runs just fine.

class name and file name should be same

File name and class name same in Java - Without public class

Now let's take an example when there is no public class-

class FinalClass{
  String a;
  final void finalMethod(){

  }
}

class FinalClassDemo {
  public static void main(String[] args) {

  }
}

I have this file with 2 classes and none of them is public, now I can save it giving any name, let's say I saved it as ABC.java. Yes, it is possible if there is no class with access modifier as public. But that is only half of the truth! When this java file is compiled it will create 2 classes-

FinalClassDemo.class
FinalClass.class

It can be seen that even if the file name is different compiled classes have the same name as the class names.

Now if we have to run it then we have to use-

java FinalClassDemo

This shows that at compile time file name may be different from the class name in Java (provided there is no public class in the file) but at the run time it has to be same.

Why these restrictions

Now let’s try to see why this restriction to have file name same as class name for public class in Java? One scenario which I can think of where it makes sense is when you have a Java file with one public class and other classes too. You must be knowing that you can have a file in Java with at most one public class and other classes with default access (package-private).

Let’s see it with an example, suppose I have a java file as follows–

public class Test{
 public static void main(String[] args) {
  System.out.println("in test class");
  //Test1 t = new Test1();
 }
}

class Test1{
 public void display(){
  System.out.println("in Test1 class");
 }
}

class Test2{
 public void display(){
  System.out.println("in Test2 class");
 }
}

Here we have one public class Test and two other classes Test1 and Test2 having default access. Now, let’s say we don’t have the restriction to save this file as Test.java (which is the name of the public class). So, you saved it as ABC.java. Now at the time of compilation (javac ABC.java) compiler has to scan the whole file structure to find out the public class which is surely an overhead and easily avoidable by saving the Java file as Test.java.

Coming to another point that a class with default access can be saved with different name but the compiled .class file will have the same name as the class name.

Here again let’s try to understand it with an example–

Let’s say I have one class Test1 with Default access and I have saved it as XYZ.java

class Test1 {
 public void display(){
  System.out.println("in Test1 class");
 }
}

And another class where Test1 class object is created.

public class Test{
 public static void main(String[] args) {
  System.out.println("in Test class");
  Test1 t = new Test1();
  t.display();
 }
}

In this class when you are creating the object of class Test1 you are using the name of the class (Test1 t = new Test1();) not the saved filename (XYZ.java).

Test will look for a .class file Test1 when it has to load Test1 class. If .class file also won’t have the same name as the class then JVM must maintain some internal structure to have that mapping from saved file name to class name and refer that mapping every time to load the correct class. That again is an overhead so better to have the easy solution to generate at least the .class file with the same name as class name even in the case of class which is not public.

Well the gist is better to always have the same name for the java file as the class name. For academic purpose, you may fiddle with the file name Vs class name to see what is permitted and what is restricted.

Points to note-

  • If there is no public class then file name may be different from the class name in Java.
  • In case there is a public class then it is enforced that the file name is same as the public class.
  • Even, in the case, where the file name is different, after compilation .class files have the same name as the class names.
  • Having the same name as class name is how the JVM will know which class to load and where to look for entry point (main method).

Recommendations for learning (Udemy Courses)

  1. Java Programming Masterclass Course
  2. Java In-Depth: Become a Complete Java Engineer!
  3. Spring Framework Master Class Course
  4. Complete Python Bootcamp Course
  5. Python for Data Science and Machine Learning

That's all for this topic Why Class Name and File Name Should be Same in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Java Pass by Value or Pass by Reference
  2. What are JVM, JRE and JDK in Java
  3. Object in Java
  4. Just In Time Compiler (JIT) in Java
  5. Core Java Basics Interview Questions And Answers

You may also like-

  1. Class in Java
  2. final Keyword in Java With Examples
  3. Automatic Numeric Type Promotion in Java
  4. How HashMap Works Internally in Java
  5. Difference between ArrayList and CopyOnWriteArrayList in Java
  6. Functional Interfaces in Java
  7. Fail-Fast Vs Fail-Safe Iterator in Java
  8. String in Java Tutorial