Do you make sure the file name
is the same as the class name
?
- HelloWorld.java -> file name;
- HelloWorld -> class name;
A simple demo would be:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
In your case, if you have to use the word main
as the class name
. Then it's still fine.
You have two options to achieve that:
- make sure the
file name
is the same as the class name
.
or just remove the keyword public
as follows:
class main {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Here is another thing I have to say:
Please follow Java naming convention:
- Class names should be UpperCamelCase;
- lowerCamelCase for variables, attributes, package, methods;
UPDATE
Why are the rules?
There is an answer.
Any class which is declared public should be kept in a file of the same name. It is a compilation error if the names of the public class and the file which contains it are different. A class which isn't declared public can be kept in a file of a different name.
Note that the generated class files are named after the java classes, not the file names.
Why the rules?
As for the design theory/purpose, here is a very comprehensive discussion.
But personally, I'd prefer this one by BlueRaja - Danny Pflughoeft:
It is just the convention set by Sun, the makers of Java.
The purpose is organization; the reason is so that everyone who codes in Java will have a consistant way of naming files.