File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

about class declaration

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
following class is in a file named, 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){
public static void main(String argv[]){
Arsho, Ayan
Ranch Hand

Joined: Nov 14, 2001
Posts: 60
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.
Michael Ernest
High Plains Drifter

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
I agree. Here's the link:
subject: about class declaration
It's not a secret anymore!