Java-file-name-vs-class-name

Java: Whether File Name Should Be Same as Class Name?

Understand whether a Java file name can be different from a class name. Let’s understand Java class name rules and Java file name rules with the help of different scenarios.

Java File Name vs Class Name
Java File Name vs Class Name

File Name vs Class Name

We can have any number of classes in one Java program but it can contain at most zero or one public class.

  • If no public class is available in a file then we can give any name to the Java file.
  • If a public class is available, in that case, the name of the Java file should be the same as the class name. This is mandatory or else we will get compile time error.


Scenario 1: One Public Class

class A {}

public class B {}

class C {}

class D {}
Java

In the above file, we have kept class B public. Now, if we save the file with the name B.java then no error will be encountered but if we give any other name to the file like A.java / C.java / D.java / Example.java then we will get compile time error.

D:\Java>javac A.java
A.java:3: error: class B is public, should be declared in a file named B.java
public class B {}
       ^
1 error
CMD


Scenario 2: Two Public Classes

class A {}

public class B {}

public class C {}

class D {}
Java

In the above file, we have more than one public class. Now, if we save the file with the name B.java / C.java then we will get compile time error. The filename is valid but the problem is with multiple public classes in a single file.

D:\Java>javac B.java
B.java:5: error: class C is public, should be declared in a file named C.java
public class C {}
       ^
1 error
CMD


Scenario 3: No Public Class

A class that contains the main method and name of the program, there is no relation at all.

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

class B {
  public static void main(String args[]) {
    System.out.println("B class main method");
  }
}

class C {
  public static void main(String args[]) {
    System.out.println("C class main method");
  }
}

class D {}
Java

We have four classes(no public class) in a file inside which three classes contain the main method and the fourth class does not contains anything. In this case, we can save the file with any name like Example.java.

When we compile the Java file, for every class present in a file, a separate .class file will be generated. In this case, four class files will be generated A.class / B.class / C.class / D.class. Example.class will not be generated in this case since it was only the name of the file.

When we run the program, main method of which class will be executed?

While running the Java program, we need to specify the name of the Java class, in that case, the main method of that particular class will be executed.

D:\Java>java A
A class main method

D:\Java>java B
B class main method

D:\Java>java C
C class main method
CMD

Now, if we try to run the D class then we will get an error stating the main method is not available for this class.

D:\Java>java D
Error: Main method not found in class D, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
CMD

Now, if we try to run the Example class(which is not available) then we will get an error stating Example class file is not available.

D:\Java>java Example
Error: Could not find or load main class Example
Caused by: java.lang.ClassNotFoundException: Example
CMD

Conclusion

Considering the above scenarios, we can conclude that class names and file names can be different.



Top Picks for Learning Java

Explore the recommended Java books tailored for learners at different levels, from beginners to advanced programmers.

Disclaimer: The products featured or recommended on this site are affiliated. If you purchase these products through the provided links, I may earn a commission at no additional cost to you.

1
Java: The Complete Reference
13th Edition

Java: The Complete Reference

  • All Levels Covered: Designed for novice, intermediate, and professional programmers alike
  • Accessible Source Code: Source code for all examples and projects are available for download
  • Clear Writing Style: Written in the clear, uncompromising style Herb Schildt is famous for
2
Head First Java: A Brain-Friendly Guide

Head First Java: A Brain-Friendly Guide

  • Engaging Learning: It uses a fun approach to teach Java and object-oriented programming.
  • Comprehensive Content: Covers Java's basics and advanced topics like lambdas and GUIs.
  • Interactive Learning: The book's visuals and engaging style make learning Java more enjoyable.
3
Modern Java in Action: Lambdas, streams, functional and reactive programming
2nd Edition

Modern Java in Action: Lambdas, streams, functional and reactive programming

  • Latest Java Features: Explores modern Java functionalities from version 8 and beyond, like streams, modules, and concurrency.
  • Real-world Applications: Demonstrates how to use these new features practically, enhancing understanding and coding skills.
  • Developer-Friendly: Tailored for Java developers already familiar with core Java, making it accessible for advancing their expertise.
4
Java For Dummies
8th Edition

Java For Dummies

  • Java Essentials: Learn fundamental Java programming through easy tutorials and practical tips in the latest edition of the For Dummies series.
  • Programming Basics: Gain control over program flow, master classes, objects, and methods, and explore functional programming features.
  • Updated Coverage: Covers Java 17, the latest long-term support release, including the new 'switch' statement syntax, making it perfect for beginners or those wanting to brush up their skills.

Add a Comment

Your email address will not be published.