1. Lesson: Language Basics
  2. Variables
    1. Naming
    2. Primitive Data Types
    3. Default Values
    4. Literals
    5. Using Underscore Characters in Numeric Literals
    6. Arrays
    7. Declaring a Variable to Refer to an Array
    8. Creating, Initializing, and Accessing an Array
    9. Copying Arrays
    10. Summary of Variables
    11. Questions and Exercises: Variables
  3. Expressions, Statements, and Blocks
    1. Expressions
    2. Statements
    3. Blocks
    4. Questions and Exercises: Expressions, Statements, and Blocks
  4. Control Flow Statements
    1. The if-then Statement
    2. The if-then-else Statement
    3. The switch Statement
    4. Using Strings in switch Statements
    5. The while and do-while Statements
    6. The for Statement
    7. The break Statement
    8. The continue Statement
    9. The return Statement
    10. Summary of Control Flow Statements
    11. Questions and Exercises: Control Flow Statements
  5. Lesson: Classes and Objects
    1. Classes
    2. Declaring Classes
    3. Declaring Member Variables
    4. Access Modifiers
    5. Types
    6. Variable Names
    7. Defining Methods
    8. Naming a Method
    9. Overloading Methods
    10. Providing Constructors for Your Classes
    11. Passing Information to a Method or a Constructor
    12. Parameter Types
    13. Arbitrary Number of Arguments
    14. Parameter Names
    15. Passing Primitive Data Type Arguments
    16. Passing Reference Data Type Arguments
    17. Objects
    18. Creating Objects
    19. Declaring a Variable to Refer to an Object
    20. Instantiating a Class
    21. Initializing an Object
    22. Using Objects
    23. Referencing an Object's Fields
    24. Calling an Object's Methods
    25. The Garbage Collector
    26. More on Classes
    27. Returning a Value from a Method
    28. Returning a Class or Interface
    29. Using the this Keyword
    30. Using this with a Field
    31. Using this with a Constructor
    32. Controlling Access to Members of a Class
    33. Understanding Instance and Class Members
    34. Class Variables
    35. Class Methods
    36. Constants
    37. The Bicycle Class
    38. Initializing Fields
    39. Static Initialization Blocks
    40. Initializing Instance Members
    41. Summary of Creating and Using Classes and Objects
    42. Questions and Exercises: Classes
    43. Questions and Exercises: Objects
  6. Nested Classes
    1. Why Use Nested Classes?
    2. Static Nested Classes
    3. Inner Classes
    4. Inner Class Example
    5. Local and Anonymous Inner Classes
    6. Modifiers
    7. Summary of Nested Classes
    8. Questions and Exercises: Nested Classes
  7. Enum Types
    1. Questions and Exercises: Enum Types
  8. Annotations
    1. Documentation
    2. Annotations Used by the Compiler
    3. Annotation Processing
    4. Questions and Exercises: Annotations
  9. Lesson: Interfaces and Inheritance
    1. Interfaces
    2. Interfaces in Java
    3. Interfaces as APIs
    4. Interfaces and Multiple Inheritance
    5. Defining an Interface
    6. The Interface Body
    7. Implementing an Interface
    8. A Sample Interface, Relatable
    9. Implementing the Relatable Interface

6.4.Inner Class Example #

To see an inner class in use, let’s first consider an array. In the following example, we will create an array, fill it with integer values and then output only values of even indices of the array in ascending order.
The DataStructure class below consists of:
• The DataStructure outer class, which includes methods to add an integer onto the array and print out values of even indices of the array.
• The InnerEvenIterator inner class, which is similar to a standard Java iterator. Iterators are used to step through a data structure and typically have methods to test for the last element, retrieve the current element, and move to the next element.
• A main method that instantiates a DataStructure object (ds) and uses it to fill the arrayOfInts array with integer values (0, 1, 2, 3, etc.), then calls a printEven method to print out values of even indices of arrayOfInts.

public class DataStructure {
    //create an array
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];
    public DataStructure() {
        //fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
    public void printEven() {
        //print out values of even indices of the array
        InnerEvenIterator iterator = this.new InnerEvenIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + " ");
//inner class implements the Iterator pattern
    private class InnerEvenIterator {
        //start stepping through the array from the beginning
        private int next = 0;
        public boolean hasNext() {
            //check if a current element is the last in the array
            return (next <= SIZE - 1);
        public int getNext() {
            //record a value of an even index of the array
            int retValue = arrayOfInts[next];
            //get the next even element
            next += 2;
            return retValue;
    public static void main(String s[]) {
        //fill the array with integer values and print out only values of even indices
        DataStructure ds = new DataStructure();

The output is:
0 2 4 6 8 10 12 14
Note that the InnerEvenIterator class refers directly to the arrayOfInts instance variable of the DataStructure object.
Inner classes can be used to implement helper classes like the one shown in the example above. If you plan on handling user-interface events, you will need to know how to use inner classes because the event-handling mechanism makes extensive use of them.

Suggest Edit