Myth about the file name and class name in Java

17 Feb, 2021
The first lecture note given during java class is “In java file name and class name should be the same”. When the above law is violated a compiler error message will appear as below 
Java
/***** File name: Trial.java ******/public class Geeks {    public static void main(String[] args)    {        System.out.println("Hello world");    }}
Output:  
javac Trial.java
Trial.java:9: error: class Geeks is public, should be
                    declared in a file named Geeks.java
public class Geeks
^
1 error 



But the myth can be violated in such a way to compile the above file.  
Java
/***** File name: Trial.java ******/class Geeks {    public static void main(String[] args)    {        System.out.println("Hello world");    }}
Step 1:  
javac Trial.java



Step 1 will create a Geeks.class (byte code) without any error message since the class is not public.Step 2:  
java Geeks



Now the output will be Hello worldThe myth about the file name and class name should be same only when the class is declared in public.The above program works as follows:  
Now, this .class file can be executed. By the above features, some more miracles can be done. It is possible to have many classes in a java file. For debugging purposes, this approach can be used. Each class can be executed separately to test their functionalities(only on one condition: Inheritance concept should not be used). But in general, it is good to follow the myth.Example 1:  
Java
/*** File name: Trial.java ***/class ForGeeks {    public static void main(String[] args)    {        System.out.println("For Geeks class");    }}  class GeeksTest {    public static void main(String[] args)    {        System.out.println("Geeks Test class");    }}
When the above file is compiled as javac Trial.java this will create 2 .class files as ForGeeks.class and GeeksTest.class . Since each class has separate main() stub they can be tested individually.  
When java ForGeeks is executed the output is For Geeks class.
When java GeeksTest is executed the output is Geeks Test class.
Example 2:  
Java
// Program to find area of triangleclass Triangle {    int x, y;    void printArea()    {        System.out.println("Area of triangle is: " + x * y / 2);    }}  class Demo {    public static void main(String args[])    {        // Object creation        Triangle t = new Triangle();        t.x = 10;        t.y = 13;        t.printArea();    }}
Note: There are two classes here, Triangle and Demo. Then which class name must be considered as the file name? The class name Demo must be taken as the file name. The reason behind taking Demo as the file name is that it has the main method and execution begins from the main method.  
Filename: Demo.java
Compilation: javac Demo.java
Run: java Demo
Output: Area of triangle:65



Myth about the file name and class name in Java | GeeksforGeeks - YouTubeGeeksforGeeks532K subscribersMyth about the file name and class name in Java | GeeksforGeeksWatch laterShareCopy link3/9InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:52•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=2q_LoVmev7g" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Sowmya.L.R. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
mangalgiaishwarya2
smundada06
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.