I am being asked this question:
What are the two of the three cases where a class can use other classes without requiring import statements?
i answered with this:
) By referring to the class by package name.
2) Importing them through the source file.
He told me it was wrong but I am not sure what other cases there would be.
Jayesh A Lalwani wrote:Actually there are 4
a) same package
b) inner class (which is technically in same package.. so above 2 can be considered the same)
d) explicitly using the package name with the class name
Considering that this is likely for a beginner's class, I would safely rule out reflection and the inner classes response as the answers the instructor is looking for. The third one is likely for the case were the class to be imported resides in the java.lang package.
kenny gill wrote:What are the two of the three cases where a class can use other classes without requiring import statements?
and you answer:
kenny gill wrote:2) Importing them through the source file.
that your instructor refuses the answer, because that answer is exactly what he excluded in the question.
Vinod Tiwari wrote:Static class don't require import.
I assume you mean nested class here? (Since you can't have top-level static classes). It's not actually true though - the cases where you need to import a static nested class are exactly the same as where you need to import any other class. That is, it's in a different package (other than java.lang), and you're not using the fully-qualified name.