views:

94

answers:

3

For instance, a method returns an object of type List.

public List<Foo> bojangles ()
...

Some piece of code calls the method FooBar.bojangles.iterator();

I'm new to Java, but from what I can tell.. List is an interface, so the iterator method has to be implemented somewhere else. When digging for the source code for Java.Util and that's exactly what I found, an iterface. Iterator itself is an iterface...so somewhere, I'm guessing there are classes or abstract classes being called that actually have the logic for these methods: next() and hasNext(). But where?

+3  A: 

Take a look at the javadoc for List. It gives a list of known implementing classes. Each of these classes must implement the iterator() method.

If you dig into the source code for those classes, you'll find that they generally implement the Iterator interface in a private nested class.

Aaron Novstrup
+2  A: 

The concrete class does the real job.

In Eclipse, you could run debug mode: run your code step by step and watch 'Variables' view. Then you could find what exactly takes place in runtime.

Or you could print out getClass() of the List to console, then check source.

卢声远 Shengyuan Lu
If you're curious, and want to understand more about how Java works, then yes. But in everyday programming, just think "it's a List" not "it's some kind of object that implements the List interface" and you can trust that the list will do what you expect, despite the fact that you have no idea how it's implemented. That's what's so cool about using interfaces!
MatrixFrog
@MatrixFrog: Agree! that's interface's value, it's a contract indeed.
卢声远 Shengyuan Lu
+2  A: 

At first your question confused me, but I think you meant to say FooBar.bojangles().iterator(). That would make more sense (note the added parentheses).

The method bojangles() returns an object, that implements List. For example, it might be an ArrayList or LinkedList.

By calling the iterator() method on either of these lists, you will be returned an Iterator. This Iterator was created in the SimpleListIterator inner class of AbstractList (that's the actual source code, and in it you will find the implementations you were looking for).

So, by calling the iterator() method, you are most likely getting a SimpleListIterator. Although this might not be true in all cases, it will be true in most.

Serplat
The explanation is correct, but the details are implementation-dependent. SimpleListIterator is an Apache Harmony thing; that's just one implementation, and most people use something else like Sun's JDK which does it differently.In fact it's more common for each list class to implement its own Iterator, instead of inheriting from AbstractList; the whole point of all this is that you can implement an efficient iterator that uses internal details of your class.
Porculus
Thank You - after your post, I went down the rabbit hole and was able to find that after several sequential method calls it was returning an arraylist. However, upon inspection of the javadocs for AbstractList there is no mention of any inner classes. Looking into the source code, the SimpleListIterator is indeed there, why is there no mention in the javadocs? Is this common in Java, inner classes are not documented?
gnugrf
There is no mention of the inner classes in the javadocs because they are implementation details. The caller doesn't need (and in fact doesn't generally *want*) to know about them. The caller only needs to know that they adhere to the contract specified by the `Iterator` interface.
Aaron Novstrup
Thanks everybody. *the door opens a bit more*
gnugrf