Your StackIterator class is an inner Stack class. This especially means that it is not static. Therefore, he already knows about the parameter of type Item .
You make a mistake when StackIterator has its own parameter of type Item . This obscures the type parameter of the outer class.
So, just remove the type parameter from StackIterator :
public class Stack<Item> implements Iterable<Item> { private class Node { Item item; Node next; } private Node first = null; @Override public Iterator<Item> iterator() { return new StackIterator(); } private class StackIterator implements Iterator<Item> { private Node current = Stack.this.first; @Override public Item next() { Item item = this.current.item; this.current = this.current.next; return item; } } }
source share