Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about class declaration

 
david hu
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic