• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query: When should the main class be declared as public & When should a class be declared as public?

 
raul saini
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1: When should the main class be declared as public?

Ques 2: When should a class be declared as public?

Ques 3: Why we can not declare 2 classes public?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a main class? The main method must be declared public for access, but the class with the main method in may have package-private access as long as it is accessed from inside the same directory. [Probably also protected access if it is an inner class, but I have never tried.]
Classes should be declared public if access from outside their package is required. You can have as many public classes as you like, they only must be in their own files. If you search through JavaRanch, you will find more details, but it has to do with easier access to the code for the compiler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic