How Do You Troubleshoot a Java Compilation Error “class fooBar is public, should be declared public class…”?

Problem scenario
You try to compile a java program, but you get this error:

hello.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
^
1 error

What is wrong?

Solution
The .java file must have a name of "HelloWorld" like the public class in the error. If the .java file is called something else, it will not compile. The name must be case sensitively the same.

If you want to purchase a book on Java, you can click here.

Leave a comment

Your email address will not be published. Required fields are marked *