2

I'm new to Java and code below is from Java tutorial Oracle.

I'm puzzled by two questions

1) Could someone please tell me what the "this" keyword is referring to within the context of

DataStructureIterator iterator = this.new EvenIterator();

I have deleted 'this' keyword from the statement and everything seems to be working fine. Does 'this' keyword services some special function I am unaware of or is it redundant?

2) What is the use of

interface DataStructureIterator extends java.util.Iterator<Integer> { }

Is it really necessary? Because I have deleted it from the code (and several minor related changes) and everything works just fine.

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
        DataStructureIterator iterator = this.new EvenIterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    interface DataStructureIterator extends java.util.Iterator<Integer> {
    }

    // Inner class implements the DataStructureIterator interface,
    // which extends the Iterator<Integer> interface
    private class EvenIterator implements DataStructureIterator {

        // Start stepping through the array from the beginning
        private int nextIndex = 0;

        public boolean hasNext() {

            // Check if the current element is the last in the array
            return (nextIndex <= SIZE - 1);
        }

        public Integer next() {

            // Record a value of an even index of the array
            Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);

            // Get the next even element
            nextIndex += 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();
        ds.printEven();
    }
}
Thor
  • 9,638
  • 15
  • 62
  • 137
  • 1
    It refers to the current class. If you are in an anonymous class it would be refer to the anonymous class. – SomeJavaGuy Jan 05 '16 at 12:11
  • 1
    For your second question, it is unnecessary if the class you are refering to is imported. It´s only really neccessary if you are using two classes with the same name in one class. You could import one of these, and the other would have to have the exact path the the actuall class. – SomeJavaGuy Jan 05 '16 at 12:17

1 Answers1

2

DataStructureIterator extends java.util.Iterator<Integer> without adding any new methods. Therefore any place that uses it can be safely replaced with java.util.Iterator<Integer>.

The this in this.new EvenIterator() refers to the current DataStructure instance that serves as the enclosing instance of the instance of the inner EvenIterator class that is being instantiated in that statement. Since you are creating the instance of EvenIterator from within an instance of the enclosing class DataStructure, there's no need to specify it explicitly, and new EvenIterator() works.

Eran
  • 387,369
  • 54
  • 702
  • 768