Something that’s confused me – an example:
Thing.java:
import java.util.Date; class Thing { static Date getDate() {return new Date();} }
(same package) TestUsesThing.java:
// not importing Date here. public class TestUsesThing { public static void main(String[] args) { System.out.println(Thing.getDate().getTime()); // okay // Date date = new Date(); // naturally this wouldn't be okay } }
Why is it not necessary to import Date to be able to call getTime() on one of them?
Importing in Java is only necessary so the compiler knows what a
Dateis if you typeImporting is not like
#includein C/C++; all types on the classpath are available, but youimportthem just to keep from having to write the fully qualified name. And in this case, it’s unnecessary.