Skip navigation.
Home

Inner Classes

A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class's members even if they are declared private. Nested classes  Can access all fields  Can be static  You can declare an inner class without naming it public class Stack { private Object[] items; //code for Stack's methods and constructors not shown public Iterator iterator() { return new Iterator() { int currentItem = items.size() - 1; public boolean hasNext() { ... } public Object next() { ... } public void remove() { ... } } } }  A nested class declared within a method or other smaller block of code has access to any final or local variables in scope.