0

I've the following question:

Why is it not necessary to use a name of the list before the add-statement (add (pipedInstanceIterator.next())) ?

Thanks.

public class InstanceList extends ArrayList<Instance> implements Serializable, Iterable<Instance>, AlphabetCarrying
...
public void addThruPipe (Iterator<Instance> ii)
{
    //for debug
    Iterator<Instance> pipedInstanceIterator = pipe.newIteratorFrom(ii);
    while (pipedInstanceIterator.hasNext())
    {   
        add (pipedInstanceIterator.next());
        //System.out.println("Add instance " + pipedInstanceIterator.next().getName());
    }
}
1
  • 2
    You're extending ArrayList, so the method is implicitly called on this. Commented Jun 11, 2015 at 14:26

2 Answers 2

1

Your InstanceList is a sub-class of ArrayList, so it can execute any public or protected methods of that class without specifying the instance for which they should be executed.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay. One last question: Is it because of the Collection ArrayList or does it apply in general (relationship supper - sub class)?
@user3680103 it applies to any sub-class.
But why does it not work if I call the add-method at the main-method and it gives the warning "cannot cast a static to a non-static reference" ?
@user3680103 Because the main method is static, so it doesn't have a reference to any instance of InstanceList.
0

Because InstanceList extends ArrayList, any unspecified methods will be run on it. So your adding to instance list

2 Comments

But why does it not work if I call the add-method at the main-method and it gives the warning "cannot cast a static to a non-static reference" ?
In what context do you get this error? It usually happens when you try to call a method from class(add), not from object of that class (e.g. InstanceList)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.