This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
jQuery in Action, 2nd edition
 
subject: about class declaration
 
Similar Threads
Instance blocks.
For loop
Object.class
inner class
Anaonymous class intialiser exception