DEV Community

Kodlogs
Kodlogs

Posted on • Originally published at kodblems.com

Java Error - at least one public class is required in main file

Problem:
Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice:

class Institutions {
public void print_institutions()
{
System.out.println("This is an institution");
}
}
class School extends Institutions{
public void print_school()
{
System.out.println("This is a school");
}
}
class Academy extends School {
public void print_academy()
{
System.out.println("This is an academy");
}
}
class Call{
public static void main(String[] args) {
Institutions institute = new Institutions();
School school = new School();
Academy academy = new Academy();

      institute.print_institutions();
      school.print_school();
      academy.print_academy();
 }
Enter fullscreen mode Exit fullscreen mode

}
When I run this program, the compiler throws the following error:

Error - At least one public class is required in main file
I have never seen this type of error before, therefore,
I have no idea what is causing this error.

Can anybody here please clarify the cause of the error and its solution? Thanks!

Solution:
Your program does not have any issues.
If you try running it on an online compiler, it does not generate any error. But, the solution to this error is quite simple,
as the error itself says that there must be a public class in the main file,

then you must set the access modifier of your "Call" class to the public. Once your "Call" class is public, this error will not be thrown by the compiler.

The other solution to this error,
in general, is to set the name of your file the same as the public class of your program.

For instance, if you have a file named "Test.java" and you have a public class named "Hello", this error will generate in this scenario too. So, try to save your file with the public class’s name.

I hope this will help you
Thanks

Discussion (0)