in java, filename should be same as that of main class. It is the way of telling compiler that this is the entry point for you. but why this thing works:
class xyz{
public static void main(String[] args){
System.out.println("a");
}
}
even when saved with different filename.
And why this thing does not when saved with diffrent filename:
public class xyz{
public static void main(String[] args){
System.out.println("a");
}
}
public classes have to be in a file with the correct filename. Non-public classes can be in any file you want. Even multiple classes in the same file if it is convenient.
Note that:
class xyz
Is not a public class so it cannot be acessed from outside of the file. Therefor it does not need to have the same name. But in this case:
public class xyz
You do have a public classe, that it gonna be acessed from outside of the file, so it does need to have the same name.
Conclusion: public classes need to have the file name exatly the same as the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With