Actually this the first time I try this out. But it works just fine.
Thinking it over, the reason is that a single file of java source code can hold
any number of classes but with one condition. This condition is that ONLY ONE
class can be declared as public. And because there are no public class in the
code you provided as an example, you can name this file after any calss name in
this file.
That's number one.
Number two, you can write as many main methods as you like with any signature
you want. but note that the only signature the JVM realy invokes is with the
signature:
public static void main(String[] args) { }
So, if you provided a method like the following
static void main(String[] args)
The JVM will not invoke this method ecause it's not public. Also, a public main
method but not static will not be invoked. Even if you tried out the following
code, it will not work:
public static void Main(String[] args) { }
** note that the method name is Main not main. Java is case sensitive.
There is one last note you must take care of while you are applying different
classes, each with it's main method, in the same source code file.
If you named the example you gave with the name (x.java), running the x.class
file will run the main method of the x class. if you tried running the y.class,
then you are invoking the main method of the y class, not x.