I thought members of an inner class, even when declared private, is accessible from its enclosing class. But I am running into compile time error with the following code structure. My top level class implements Iterable interface. The Iterator is implemented in an inner class. When an instance of the inner class is obtained through the iterator() method, I am not able to access the data field with that instance.
public class RandomQueue<Item> implements Iteralbe<Item>
{
public RandomQueue() {}
public Iterator<Item> iterator() // iterator() method
{
return new ShuffleIterator();
}
// inner class implementing iterator
private class ShuffleIterator implements Iterator<Item>
{
private int i; // private data field in inner class.
.......
public boolean hasNext() {...}
public Item next() {...}
public void remove() {...}
}
public void doSomething()
{
// Compile time error. "i cannot be resolved or is not a field"
int j = iterator().i;
}
}
Any suggestions?
Iterator
does not have an accessible field namedi
. What do you think you're accessing withiterator().i
? – Sotirios Delimanolisprivate
. – user253751