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: javac Trial.java
Step1 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 world
The myth about the file name and class name should be same only when the class is declared in
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 will create two .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.
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
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java