Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Query: When should the main class be declared as public & When should a class be declared as public? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query: When should the main class be declared as public & When should a class be declared as public?" Watch "Query: When should the main class be declared as public & When should a class be declared as public?" New topic
Author

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

raul saini
Greenhorn

Joined: May 18, 2010
Posts: 18
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

Joined: Oct 13, 2005
Posts: 37926
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query: When should the main class be declared as public & When should a class be declared as public?
 
Similar Threads
Interesting JCP Ques
ques
Ques from Abhilash 'ssite
doubts in mock exam ques
J2eeCertificate ques