Why is import of class not needed when calling method on instance (Java)

前端 未结 5 978
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 15:37

Something that\'s confused me - an example:

Thing.java:

import java.util.Date; 

class Thing { 
    static Date getDate() {return new Date();}
}
         


        
5条回答
  •  青春惊慌失措
    2020-12-17 15:54

    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.

提交回复
热议问题