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
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.
Step 1 will create a Geeks.class (byte code) without any error message since the class is not public.
Now the output will be Hello world
The myth about the file name and class name should be same only when the class is declared in public.
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.
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.
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
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 firstname.lastname@example.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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Using predefined class name as Class or Variable name in Java
- Java.io.File Class in Java
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Difference between Abstract Class and Concrete Class in Java
- How to run java class file which is in different directory?
- Java Class File
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- In Java, Can we call the main() method of a class from another class?
- Inner Class And Anonymous Inner Class that Implements Runnable | Concurrent Programming Approach 3
- Does JVM create object of Main class (the class with main())?
Improved By : mangalgiaishwarya2