DEV Community

CoderLegion
CoderLegion

Posted on • Edited on • Originally published at kodblems.com

2 1

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

🎉 Before you dive into this article...

🚀 Check out our vibrant new community at CoderLegion.com!

💡 Share your knowledge, connect with like-minded developers, and grow together.

👉 Click here to join now!

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

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay