• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Could someone tell me why instructor refused my answer?

 
kenny gill
Ranch Hand
Posts: 54
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by importing through source file?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.forName(...)

It is a question I always ask in interviews.

WP
 
dennis deems
Ranch Hand
Posts: 808
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An import statement is not required if the other class resides in the same package.
 
kenny gill
Ranch Hand
Posts: 54
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, I guess another question would be what are the 3 cases?
 
Jayesh A Lalwani
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20825
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static class don't require import.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic