• 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

Inner class terminology

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please clarify the following... inner class vs. nested class.

According to Heller & Robers (Complete Java 2 Certification), "Inner classes, which are sometimes referred to as nested classes, can give your programs additional clarity and make them more concise."

However, according to a question on Dan's mock exams, "An inner class is a nested class that is not static."

Which definition is correct? H&R seem to define the two terms as interchangable. I'm a little concerned if H&R is wrong, because I've been using this book to study...
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two quotes from the Java language Specification:

A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class





An inner class is a nested class that is not explicitly or implicitly declared static

 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very perceptive of you. You're not going to like this. It's a minefield.

The official terminology as defined by the Java Language Specification (JLS) is that inner classes are non-static nested classes. In other words, an inner class always carries with it an implicit reference to an object of the enclosing class. This is the terminology you can expect in the exam.

This should have been the end of the story. Unfortunately, as you found, many people - including Sun itself in the early days - use the terms "inner class" and "nested class" interchangeably. And everyone has remained horribly confused ever after. So whenever you come across these terms you should always be wary about their precise meaning.

Having said that, I do firmly believe that any SCJP book should use the JLS terminology throughout, and a good tech editor should have caught that in the HR book.

- Peter
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct me if i'm wrong, but...

all inner classes are nested classes, but not all nested classes are inner classes. from the book java 2 programmer exam cram, by brogden and green, "Generally speaking, a nested class is a member of another class. However, it is common to speak of the static members as nested and the nested classes that are members of instances of the enclosing class as inner classes. Indeed, you are most likely to read about inner classes only, but the more general term is nested classes." the thing is, static member classes are pretty rare, so it seems like they are nearly inter-changable when technically they're not.

hope that helps.
[ August 23, 2004: Message edited by: Chris Wash ]
 
Ken Weller
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification. I'm very disappointed that the book is incorrect, because the entire section on inner classes is useless as they base most of the material on their own wrong definition.

Thank God for Dan's mock exams and the ranch , or I may have lost a LOT of marks on this topic simply because I bought the wrong SCJP study guide!
 
reply
    Bookmark Topic Watch Topic
  • New Topic