• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about class declaration

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
}
}
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic