| Author |
Could someone tell me why instructor refused my answer?
|
kenny gill
Ranch Hand
Joined: Mar 12, 2012
Posts: 54
|
|
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
Bartender
Joined: Jan 17, 2008
Posts: 1273
|
|
|
What do you mean by importing through source file?
|
 |
William P O'Sullivan
Ranch Hand
Joined: Mar 28, 2012
Posts: 860
|
|
Class.forName(...)
It is a question I always ask in interviews.
WP
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
|
An import statement is not required if the other class resides in the same package.
|
 |
kenny gill
Ranch Hand
Joined: Mar 12, 2012
Posts: 54
|
|
Well, I guess another question would be what are the 3 cases?
|
 |
Jayesh A Lalwani
Bartender
Joined: Jan 17, 2008
Posts: 1273
|
|
Actually there are 4
a) same package
b) inner class (which is technically in same package.. so above 2 can be considered the same)
c) Reflection
d) explicitly using the package name with the class name
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
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)
c) Reflection
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.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
|
Well, since b is completely implied by a, b is completely unnecessary. Similarly, even if you use reflection, you will need to use the full package name, so it's really just another case of a. The use of the java.lang package is the only other option that makes any sense.
|
 |
Jayesh A Lalwani
Bartender
Joined: Jan 17, 2008
Posts: 1273
|
|
Ahh yes, forgot about java.lang
I thought when he said "importing them through source file", I thought he was going for inner classes. Could be wrong. That is why I asked
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Jayesh A Lalwani wrote:d) explicitly using the package name with the class name
technically, it is an import . the work is done by compiler.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
Seetharaman Venkatasamy wrote:
Jayesh A Lalwani wrote:d) explicitly using the package name with the class name
technically, it is an import . the work is done by compiler.
But it is "without an import statement".
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
I can imagine that when the question is:
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Vinod Tiwari
Ranch Hand
Joined: Feb 06, 2008
Posts: 458
|
|
|
Static class don't require import.
|
Vinod Tiwari | Twitter
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
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.
|
 |
 |
|
|
subject: Could someone tell me why instructor refused my answer?
|
|
|