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 Date is if you type
Date date = new Date();
Importing is not like #include in C/C++; all types on the classpath are available, but you import them just to keep from having to write the fully qualified name. And in this case, it's unnecessary.