• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is the Enumeration interface's hasMoreElements method being called directly?  RSS feed

 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();)

In the above entry , entries appears to be an object from an inner anonymous class who's method inherited
from Enumeration is being called, with name hasMoreElements. But hasMoreElements has not been specified
anywhere in the code listed in the java tutorial.
What is happening here? How can the hasMoreElements method be called likewise as in above without being
specified and listed first with its code.
Please help.
Thanks,
MKZ
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whatever zf is, it has a method called entries() that returns an Enumeration. (It is a method on zf, not an object.) There does not have to be any inner class involved, anonymous or otherwise. The following tiny, useless class is a fragment that illustrates a legal call to entries returning an Enumeration.



Looking up java.util.Enumeration, you can see that any class that implements the interface must implement hasMoreElements() (as StringTokenizer does). The class for the object returned by "entries()" must have hasMoreElements() in it somewhere.

Please ask more questions if you don't get it yet. There are a couple of basic concepts here you should have down solidly before you go on to other things...

rc
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Zia wrote:Please help.

Just an additional point, FYI: Enumeration is an old class whose functionality is now duplicated by java.util.Iterator, so in general it's best to use the latter if you have a choice.
Furthermore, any java.lang.Iterable (which covers all Java Collections) can use the new 'enhanced' for loop.

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate what RC said, Enumeration is an interface, and a class which implements it must implement the hasMoreElements() method. So you are calling the hasMoreElements method of whichever class you call it on. In fact, since the method in the interface looks like this... you cannot actually call it. There is nothing to call.
What do you know about interfaces? Have a look at my Engine interface in this old thread.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, Enumeration is legacy code; you ought not to use it. Use Iterator instead.
 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

RC, Cambell,
in the code listed above, how does the iteration take place:
for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();)

generally a for loop is of the form for (i=0; i<10; i++)
can you explain the for loop above for me please??
Thanks again for your assistance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Zia wrote:in the code listed above, how does the iteration take place:
for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();)

It doesn't. Inside the for loop there will be a call to entries.nextElement. This is what causes the code to return the next element and move the 'pointer' to the next position in the collection.
 
M Zia
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,
but there is no call to entries.nextElement being made inside the for loop.
are you saying this happens automatically or hasMoreElements calls nextElement.
Please clarify.
Thanks for your assistance.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M Zia wrote:Joanne,
but there is no call to entries.nextElement being made inside the for loop.
are you saying this happens automatically or hasMoreElements calls nextElement.

Neither. If there is no call to nextElement in the loop then (assuming there is at least one item in the collection) that loop will never end.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find details about for loops in the Java Language specification. It may not answer your specific query, however.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!