Nested Classes in Java: Demonstrating Use of Inner Classes

uppose we are required to create an application that generates a random list of odd numbers. The list itself should consist of a random number of entries. For this, we will first generate a fixed number of integers. We will then iterate through these entries to filter out only the odd numbers.

We would define this filtering functionality in an inner class because this functionality would be of little use outside the scope of the current application. The purpose of declaring a class rather than a method is the ability to use this class for creating multiple lists within the application. The complete program for generating a random list of odd numbers is given here:

public class DynamicOddsGenerator { 

                           //Declare an array of 25 integers
                     private final static int SIZE = 25;
                     private int[] arrayOfInts = new int[SIZE];
                     public DynamicOddsGenerator() {
                                       //fill the array with random generated numbers
                             for (int i = 0; i < SIZE; i++) {
                                      arrayOfInts[i] = (int) (Math.random() * SIZE);
                           //print all odd numbers stored in the array
                     public void printOdds() {
                             InnerOddsIterator iterator = InnerOddsIterator();
                             while (iterator.hasNext()) {
                                      int returnValue = iterator.getNext();
                                      if (returnValue != -1) {
                                                System.out.print(returnValue + " ");
                     // inner class implements the Iterator pattern
                     private class InnerOddsIterator {
                             private int next = 0;
                             public boolean hasNext() {
                                      return (next <= SIZE - 1);
                                     // returns the number if it is odd, otherwise return -1
                             public int getNext() {
                                      int retValue = arrayOfInts[next++];
                                      if (retValue % 2 == 1) {
                                                return retValue;
                                      return -1;
                     public static void main(String s[]) {
                             DynamicOddsGenerator numbers = new DynamicOddsGenerator();                              numbers.printOdds();

Some of the numbers in the array will be odd and some will be even. The printOdds method prints all the odd numbers stored in this array. The method first creates an instance of an inner class that provides this filtering functionality:

InnerOddsIterator iterator = InnerOddsIterator();

·The InnerOddsIterator is an inner class that implements the Iterator design pattern and provides the hasNext and getNext methods.

·The InnerOddsIterator class is declared with a private modifier with the intention of keeping it totally private to the enclosing class. Generally, we will not declare the inner classes public unless we really find a valid reason for using an object of an inner class outside the scope of the enclosing class.

·The inner class declares a private field called next.

·The hasNext method returns the next index in the array provided we have not crossed the limits of the array.

·The getNext method checks whether the element value at the current index is an odd number. If so, it returns this number; otherwise, it returns –1 to the caller. Note that SIZE is a static constant defined in the outer class and is accessed within the body of the inner class.

·The main method defined in the outer class simply instantiates it and invokes the printOdds method on it.

·Note that every time we run the program, we will get a different list of odd numbers. The number of generated entries will also vary on each run.

Last updated:3/15/2018 12:50:31 AM


Leave Comment