Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Study guide: Classes within classes

Brush up on Java terms, learn tips and cautions, review homework assignments, and read Jeff's answers to student questions

  • Print
  • Feedback

Glossary of terms

anonymous inner class
An unnamed inner class located within a method block or a statement block in an object creation expression.


block
A region of source code sandwiched between open brace ({) and close brace (}) characters.


inner classes
Nested classes that you declare without the static keyword.


instance inner class
An inner class that you declare within a class block. In other words, you do not declare that class within a method block or a statement block such as an if statement block.


iterator
An object whose methods return other objects in succession from a containing object and determine if there are additional objects in the containing object to return.


local inner class
A named inner class located within a method block or a statement block.


nest
To appear within. For example, when a class nests within another class, the nested class appears within an enclosing class.


nested top-level class
A class that you declare with the static keyword in a top-level class or another nested top-level class.


synthetic field
A compiler-created field that links a local inner class to a block's local variable or reference type parameter.


top-level class
A class that has no enclosing class. In other words, the class is not a nested top-level class or an inner class.


Tips and cautions

These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.

Tips

  • You can declare either a nested top-level class or an instance inner class with the private, protected, or public keywords to determine that class's level of accessibility to code outside the enclosing class.
  • Learn more about the compiler's manipulation of local inner classes into top-level classes by studying the Inner Classes Specification document and by using the javap program to disassemble class files.


Cautions

  • A nested top-level class cannot access any enclosing class's instance members (fields and methods).
  • Attempts to declare constructors in an anonymous inner class fail because constructors must have the names of the classes in which they appear, and anonymous inner classes have no names.


Miscellaneous notes and thoughts

There is some confusion regarding nested classes and static members. That confusion results from compiler error messages that appear when the compiler discovers static field, method, or class declarations within inner classes. It turns out that only nested top-level classes can declare such members. To the best of my knowledge, the reason has something to do with keeping Java more object-oriented. To see these errors for yourself, compile the following InnerStatic source code:

// InnerStatic.java
class InnerStatic
{
   // A is a nested top-level class.
   static class A
   {
      // static field, method, and class declarations are permitted in a
      // nested top-level class.
      static int x = 1;
      static void methodA ()
      {
      }
      static class FooA
      {
      }
   }
   // B is an instance inner class.
   class B extends A
   {
      // static field, method, and class declarations are not permitted
      // in an instance inner class.
      static int y = 2; // Compiler error.
      static void methodB () // Compiler error.
      {
         System.out.println (x); // This statement is okay.
      }
      static class FooB // Compiler error.
      {
      }
      // final static field declarations are permitted in an instance
      // inner class.
      final static int Z = 3;
   }
}


InnerStatic declares nested top-level class A and instance inner class B. Each class contains three static member declarations: A declares x, static void methodA(), and FooA; whereas B declares y, static void methodB(), and FooB. Although A's declarations are legal, B's declarations are not. However, a close look at B's source code reveals two legal uses of static fields:

  • Print
  • Feedback

Resources