How Do You Troubleshoot the Java Error “class … is public, should be declared in a file named foobar.java”?

Problem scenario
You copied a Java program from the Internet.  You named the file foobar.java.  You try to compile it (e.g., with "javac foobar.java"), but you get this error:

foobar.java:6: error: class ContInt is public, should be declared in a file named ContInt.java
public class ContInt
       ^
1 error

What do you do?

Solution
Rename foobar.java to be ContInt.java.

Leave a comment

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