wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes k&b chapter 10, question 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "k&b chapter 10, question 2" Watch "k&b chapter 10, question 2" New topic
Author

k&b chapter 10, question 2

Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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.


SCJP 88% | SCWCD 84%
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
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

Joined: Aug 31, 2004
Posts: 11343

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: k&b chapter 10, question 2