I have a source file called 'dog.java' which has a public class called 'Dog'.
I know that the file name and the public class name must be the same but was just experimenting to learn something new.
when i execute the command: javac dog.java ; i get an error as expected.
but on executing: javac Dog.java ; the file gets successfully compiled ( Even though 'Dog.java' did not exist ).
This maybe due to the fact that in order to get the file handle, 'javac' uses file system interfaces which don't differentiate between upper and lower case characters ( i have checked this on windows ). Hence, it gets hold of the file ( dog.java ) and then 'javac' compares the input to it i.e. 'Dog.java' and the public class 'Dog'.
But if that is true, why does this compile successfully...
File name: 'Dog.java' public class name: 'Dog' command: 'javac dog.java'
Here, the input to 'javac' was 'dog.java' which doesn't match the class name 'Dog'.
I mean what is going on here ? Who's the villain - 'javac' or 'the file system' or 'myself' ??
*** If opportunity doesn't knock, build a door !! ***
Are you using Windows? File names on Windows are case-insenstive; as far as Windows is concerned, "dog.java" is the same as "Dog.java". However, the Java compiler is case-sensitive with regard to filenames and class names.
It's recommended to always make the filename exactly the same as the class name, also on systems like Windows that have case-insensitive filenames.
Actually the compiler is not specified to require that the file name match the public class name, although implementations may enforce that. All the spec cares about is that a top-level public class is in a .class file of the same name.