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

Top Level Public Class

 
kumar bangali
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi
I am pretty much a greenhorn and just about got started preparing for the certification exam. I need a clarification. According to RHE there can 'generally' be only one top level public class. Are there any exceptions?
------------------
Bos Indicus
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kumar,
A source file can contain more than one top level class but only one top level class can be declared public. (A top level class is not enclosed within the declaration of another class).
For example,
<code>
public class TopOne {}
class TopTwo {}
class TopThree {}
</code>
The above declarations can all appear in one source file. The file must be saved as <code>TopOne.java</code> (it has to match the name of the public top level class).
However, the following source file would produce a compile error:
<code>
public class TopOne {}
public class TopTwo {}
class TopThree {}
</code>
as it declares more than one public top level class.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
Co-author Mike Meyers' Java 2 Certification Passport
 
vishal avad
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi kumar
i just wanted to add a bit to what jane has said that
it is not neccessary that a source file must contains a public class.
rgds
vishal
 
kumar bangali
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane and Vishal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic