This file name is saved as abc.java
but it can be compiled and generates xyz.class
. Why is this possible?
class xyz {
public static void main(String args[]) {
System.out.println("hello ");
}
}
This file name is saved as abc.java
but it can be compiled and generates xyz.class
. Why is this possible?
class xyz {
public static void main(String args[]) {
System.out.println("hello ");
}
}
Because only public classes need to be named the same as the .java file.
A class which isn't declared public
can be kept in a file of a different name.
Edit: For a public
class, javac
would generate the following error:
abc.java:1: error: class xyz is public, should be declared in a file named xyz.java
public class xyz {
^
1 error