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 Top level class?? 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 "Top level class??" Watch "Top level class??" New topic

Top level class??

Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56

This is a question from a mock exam.

Given the following classes declaration in the same file

A.Two classes can never be declared in the same file.

B.The code Does't compile as the top most class is

C.The code compiles & MyClass2 can only be instantiated
in it's sub classes.

D.The code compiles & MyClass2 can only be instantiated
by the classes in the package 'mypackage'.

B is the answer. I thought top most class the the one with the widest access modifer.

Can anybody explain to me why MyClass2 is the top level class here and how to identify it?

SCJP 1.4
Abhishek Jain

Joined: Sep 02, 2005
Posts: 26
Hi Swapna

Top level classes are those which are not nested in any other class..
Let me explain it to u with an example
Class A{
Class B{
Class C{

if code is like this then
A: Top Level Class
B: Not a Top level class <==
C: Top Level Class

and haa
Top level classes can only be public or default
else the code will not compile

I think u can understand the meaning


SCJP <br />SCWCD <br />SCBCD <br />SCEA Preparing
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 980
Am not sure myself how the protected class is topmost class..

but u cannot have a class with specifier protected....

only public final static

Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Thanks Abhi,

The explanation is very clear. I got the concept.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
The given answer B is nonsense. If you've transcribed it correctly, then evidently this mock exam was written by an unreliable author. Do not spend time worryng about what the author is telling you - it's wrong; the author was confused.

A top-level class is a class that is not nested in any way. In the code shown, there are two top-level classes, MyClass1 and MyClass2. These are considered top-level, regardless of the access modifier.

You may encounter discussion of "top-level nested classes" or "static inner classes", which used to be considered top-level. This information is old (from before 2001), and should be ignored if you hear it now, as it will be unnecessarily confusing. Top-level classes are not nested. Period.

The correct answer would be that the code does not compile because, if there is more than one top-level class in a file, only one may be public, and the rest must be package access (no access modifier).

"I'm not back." - Bill Harding, Twister
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Thanks Jim,

Before the error was

Now that I changed the protected modifier the class is compling.
I agree. Here's the link:
subject: Top level class??
It's not a secret anymore!