0
// main class
class prac_1_3{
    public static void main(String[] args){
        System.out.println("Practical 3");
    }
}

I have seen it on many websites and heard that the main class should be a public modifier.

So, to confirm that I ran this above code without a public modifier and it ran successfully.

I want to know about this from you. please explain me that, in which situation a public modifier is required in terms of only main class ( i know the basics about public modifiers but I am confused with the main class file).

with public keyword and without public keyword it executed successfully.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • On the class, no. On the method, yes. – Michael Mar 20 '23 at 16:11
  • "*I have seen it on many websites and heard that the main class should be a public modifier.*" that is incorrect as your code proves. From curiosity could you point out some sources which made you think so? Maybe they meant `public` as visibility modifier *of `main` method*, not of class *holding* `main` method? – Pshemo Mar 20 '23 at 16:18

1 Answers1

0

You mentioned that on the class (not the method)

with public keyword and without public keyword it executed successfully.

That is your answer. public is not needed on the class, only on the method.

Jorn
  • 20,612
  • 18
  • 79
  • 126