1

This is something that we all take for granted, but the thought occurred to me today. Why can we do this?

package com.package


public class SomeOtherClass{

    private ClassContainingList classContainingList = new ClassContainingList();

    public void doStuff(){
        SomeClass someClass = classContainingList.getList().get(0);  
    }
}

Assume that ClassContainingList and SomeClass are also in com.package and classContainingList.getList() returns a java.util.List.

In classContainingList.getList().get(0) we are now calling methods from the List interface without ever actually importing it.

How does Java "know" about the method without an import?

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • 1
    Intuitively, I would think `getList()` would return a `java.util.List` making the import unnecessary, but I'd like to know for sure. – Christopher Schneider Sep 18 '17 at 19:09
  • 5
    `import` statements, as the duplicate post explains, simply allow you to refer to types with their short name. Since you don't need to refer to any type by name here, an `import` is not necessary. – Sotirios Delimanolis Sep 18 '17 at 19:10

0 Answers0