• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

k&b chapter 10, question 2

 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pp. 781-782 of k&b--sorry if you don't have the book.

I don't have time (library computer) to copy the question, but it involves
this source file:

class MyClass {
Robot r;
Pet p;
Dog d;
}

and asks what package and/or import statements are required for this file
to compile. A directory structure is given containing each of these class
files.

I was put off by the notion that you could import a *package* (ie a directory) and gain access to any file that appears in that package,
*apparently* without respect to whether that class actually used the
package statement to become a part of that package (directory). If
you can do that, then discussion of importing packages is sort of
misleading. I mean, each of those classes might not have any package
statement at all, and so, i believe, they'd be part of what I've heard
called the *default* package. But the upshot of that would be that, as
long as they were *found* by your classpath, you could use the *alias* (the class name without any package stuff) to
refer to the them anyway, so the file would compile without *any* of the
proposed modifications.

So the question strikes me as ambiguous.

If you suppose that each .class file shown in the diagram has a package
statement in its file that defines it as a member of a package, then the
question makes more sense--but there is the possibility that none of them do. Obviously I'm a bit confused on the issues here. Any thoughts?

Ken T.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Truitt:
... I was put off by the notion that you could import a *package* (ie a directory) and gain access to any file that appears in that package,
*apparently* without respect to whether that class actually used the
package statement to become a part of that package (directory)...

Simply being in the same directory does not make a class part of the named package. The classes must be declared as part of that package to be explicitly imported elsewhere.

The default (unnamed) package is automatically imported only if it is the current package.
[ August 06, 2008: Message edited by: marc weber ]
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if a class has to be part of a package to be imported, the question
forces us to *assume* that these classes are part of the packages
listed among the answers, right? Something doesn't seem right about
this question.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Truitt:
...the question forces us to *assume* that these classes are part of the packages
listed among the answers, right? ...

Yes. If we're going to choose from the given options, then this is implied.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic