aspose file tools*
The moose likes Beginning Java and the fly likes Could someone tell me why instructor refused my answer? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could someone tell me why instructor refused my answer?" Watch "Could someone tell me why instructor refused my answer?" New topic
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: 2431
    
  28

What do you mean by importing through source file?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

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: 2431
    
  28

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: 18977
    
  40

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: 3018
    
  10
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: 2431
    
  28

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: 4460
    
    8

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

Joined: Aug 16, 2005
Posts: 14338
    
  22

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 8 API documentation
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 466
    
    1

Static class don't require import.


Vinod Tiwari | Twitter | Shikshanirman | Guftgu
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4460
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Could someone tell me why instructor refused my answer?