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 What is a top level class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is a top level class?" Watch "What is a top level class?" New topic

What is a top level class?


Joined: Jun 24, 2000
Posts: 14
Is it simply a class which can be inherited?
Ranch Hand

Joined: May 13, 2000
Posts: 56
A top-level class is any class that is not an inner class or nested class, a class whose definition is not enclosed within another class that can live by itself in a source file, the class having unmodified (no keyword) or public access.
I'm not sure what you mean by "a class that can be inherited" because the only classes that can be inherited are classes defined inside classes (i.e., not top-level classes).
If you mean a class at the top of the inheritance hierarchy or a branch thereof (like Object, Throwable, orObjectEvent) then "no." But then I'm pretty sure whatever you mean the answer might be "no" but I'd be curious to know what you meant.
Best regards,
Steve Butcher


Joined: Jun 24, 2000
Posts: 14
A class that could be inherited, I mean, a class that is extendable, or a class that could be a super class of another class.
So the answer to my question is NO. Since final class could be
a "top level" class but it is not extendable.
I am wondering if "top level class" an official term. I have
seen "nested top level class". Does it mean only a static
inner class or any kind of inner class.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
>I'm not sure what you mean by "a class that can be
>inherited" because the only classes that can be
>inherited are classes defined inside classes (i.e.,
>not top-level classes).

Every time I think I understand all this, I read something that confuses me again. You can only extend a top level class, right?
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Dave001- no, you can extend any class as long as it's not final.
As for the original question - under the original specifications for nested classes, there is no such thing as a static inner class. The thing that everyone keeps calling a static inner class was properly called a top-level nested class. This term was used to make people aware that TLNC's had more in common with regular top-level classes than with other nested classes. (Mostly, the fact that you could instantiate them without bothering to instantiate the enclosing class first.) But the term never caught on, and in the JLS 2nd edition TLNC's are being called "static member" classes instead - they're still not properly considered inner classes, but they're not top-level either. However, people will probably continue to ignore this and talk about "static inner" classes anyway.
So, the meaning of these terms unfortunately depends on who's talking. You're probably safest assuming that someone talking about "top-level" classes does not mean to include static member classes unless they mention them specifically. If someone mentions "inner" classes, they could well mean to include static member classes as well even though that's technically incorrect. If they actually say the phrase "static inner class" (which you may see on the exam), you know that, even though it's wrong, they do mean to refer to static member classes.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: What is a top level class?
jQuery in Action, 3rd edition