We can write a java program with one non-public class containing a main method — it’ll compile and run just fine. Why do people make the main class public?
Is there any benefit?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no benefit of making the class public for the sake of it having a main method.
That being said, there isn’t really much of a reason not to either. Chances are the main class is either going to be very short with few, if any, substantial methods in it, or it’s going to be baked into one of the core classes like
And typically those core classes are something you’d want to be public.
But this isn’t about the benefits of having classes be public. This is about the benefits of having a class be public because it has the main method and there are no direct benefits as a result of that.