Why does java file name must be same as public class name?

Here, we are going to learn why does java file name must be same as public class name? Or can we keep different names for Java class name and java file name? By Preeti Jain Last updated : January 02, 2024

Can we keep different names for java class name and java file name?

Yes, we can keep the different name for the java filename and java class name if and only if the class is not public.

There are two cases, that we will discuss here...

  1. Case 1: We are taking different name if the class is not public
  2. Case 2: We are taking different name if the class is public

Case 1: Different name if the class is not public

With the help of an example, we will see what will happen if the class is not public and in that case, we can take a different name for the java class and java file that means it is not mandatory to have the same name for the java class name and java filename in that case.

Example

class ClassIsNotPublic{
    public static void main(String[] args){
        String str1 = "This class is not public so we can take different name for the java filename and java classname";
        String str2 = "This class is not prefixed with public keyword that's why it is not public";

        System.out.println("What will happen if we take non public class " +str1);
        System.out.println("Why it is not public class "+ str2);
    }
}

Output

E:\Programs>javac abc.java [abc is java filename]

E:\Programs>java ClassIsNotPublic [ClassIsNotPublic is java classname]
What will happen if we take non public class This class is not public so 
we can take different name for the java filename and java classname
Why it is not public class This class is not prefixed with public keyword 
that's why it is not public

Case 2: Different name if the class is public

If we declared a class as "public" then, in that case, java filename and java class name must be same or in other words, we can’t take any other name for the java class name and java filename in the case of a public class.

Example

public class ClassIsPublic{
    public static void main(String[] args){
        String str1 = "This class is public so we can't take different name for the java filename and java classname";
        String str2 = "This class is prefixed with public keyword that's why it is public class";

        System.out.println("What will happen if we take public class"+" " +str1);
        System.out.println("Why it is public class "+ str2);
    } 
}

Output

E:\Programs>javac xyz.java
xyz.java:1: error: class IfClassIsPublic is public, should be 
declared in a file named IfClassIsPublic.java
public class IfClassIsPublic{
       ^
1 error

Why does java file name must be same as public class name?

Now we will see why it is required to take the same name for the java filename and java class name in the case of a public class?

There are few points to understand why it is required to take the same name?

  • Let suppose we have a java file named java1000 in that Java file we have 1000 classes and in that case if we want to find any single class in 1000 classes so it will be more difficult to find and it will create a lot of confusion.
  • In that java file we have 1000 classes we know that to find any class in 1000 classes will difficult so, in that case, almost one is class will be public and that public class will contain main() method so all the classes objects will be called from main() method class (i.e. public class) so if we take java filename and public class name will be same then it will be easy to find any class in the public class.
  • If our java filename and public class name will be same so by using java filename we can easily reach to public class (main() method class) and if we reach to public class then from that class we can reach to any other class also from the 1000 classes and we know all classes object will be called from the main() method class (i.e. public class).

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.