why is iterator object not created with new keyword for list collection in Java -


i came across bit of code remove strings of length given linked list don't understand why iterator object itr not instantiated new keyword. here code..

public static void removeevenlength(list<string> list) {      iterator<string> itr= list.iterator();      while (itr.hasnext()) {          string element=itr.next();          if (element.length()%2==0) {              i.remove();      }    } }        

does mean here, iterator method static , returns new iterable object list field. can provide me 1 or more examples similar way of instantiating encountered in java other singleton constructors suppose. thank you

does mean here, iterator method static , returns new iterable object list field.

no, it's instance method. returns reference iterator<string>. body of iterator() method contain new statement (although it's possible in turn calls on else). let's take away iterators , generics - similar situation is:

class bar {}  class foo {     bar createbar() {         return new bar();     } }  public class test {     public static void main(string[] args) {         foo foo = new foo();         bar bar = foo.createbar();     } } 

same pattern: instance method returns new instance of different type.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -