Why can I call the stream() method on objects of a class that don’t have the stream()-method?

I’m a novice Java programmer at university. I discovered something today that broke one of my conceptions about how Java syntax works.

public class testClass {

ArrayList <String> persons = new ArrayList <String> ();

public void run(){
    Stream <String> personstream = persons.stream();

The method stream() is not found in the ArrayList class, yet it might appear as if it’s there. When I move my mouse over the stream()-method in Eclipse, it says it’s part of Collections, but I don’t find the stream() method anywhere in its online documentation.

Why does it work to call the stream() method if it’s not part of the class I’m calling it from?

Source: syntax

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.