I have read that the java (public) class name should match with the java file name. But why does this rule not apply when the java file does not contain a public class. I tried compiling and executing a java file (Learning.java). This file contains a class , Class Test. I expected it not to complie. But it did and worked file. Also after compiling a new Class File "Test.class" was created. I would like to know the reason.
No real reason; it's actually not even a real rule, just an optional rule that may be enforced by the implementation. The very first Java compiler did it to make it easy to find the source code for classes it hadn't compiled yet; other compilers have followed in its footsteps. But don't expect anything fancier than that.