This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about class declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about class declaration" Watch "about class declaration" New topic
Author

about class declaration

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
following class is in a file named Base.java, why I didn't put "public" before class declaration and it also could run? Is it necessary to use public before the class containing the main method
class Base{
private void amethod(int iBase){
System.out.println("Base.amethod");
}
public static void main(String argv[]){
System.out.println("OK");
}
}
Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
<code>
Well,let me try to explain. you must put a public for a class with main method if the file has more than one classes defined.
</code>
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Hi David -
It's not a requirement. A class that has "friendly" scope (no explicit declaration) is
accessible to other classes in its package. When you run the JVM in the same directory, you make the classes it initializes to run and your class part of the same default package space.
The public declaration opens access to the class from "anywhere," so while it's more liberal in scope and makes the most sense to use, it's not the only way to get to a main method.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
 
GeeCON Prague 2014
 
subject: about class declaration