Wednesday, 29 April 2015

Why class name and file name should be same in Java

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

  • File name and class name should be same.
  • Main method signature - Public static void main(String[] args)

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

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

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 should be same

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

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 as 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 (provided there is no public class in the file) but at the run time it has to be same.

So the next question is why even that restriction to have the same name as class name at run time.
Answer is that is how java interpreter will know which class to load and where is the entry point (main() method) otherwise interpreter may have to scan a lot of class files to determine where to start.

Points to note -

  • If there is no public class then file name may be different from the class name.
  • 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).

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!


Related Topics

  1. Why main method static in Java
  2. Java pass by value or pass by reference
  3. What are JVM, JRE and JDK in Java
  4. Object in Java
  5. Core Java basics interview questions

You may also like -

>>>Go to Java Basics page

3 comments:

  1. Hello Anshudeep Bajpai please add some more points in this tutorials beacuse students are not understanding this concept

    ReplyDelete
    Replies
    1. I have tried to make it more explanatory, let me know if it helps.

      Delete
  2. Good Explanation.Thanks. In java file no two Public class is allowed?Even i saved with main method classname as filename.

    ReplyDelete