• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Imports -- Question from a book I am reading

 
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Ranch Ninjas:

I am having difficulty with one of the questions at the end of the chapter related to packages. I wanted to see if others find this question confusing..

5. Given the following classes, which of the following can independently replace INSERT IMPORTS HERE to make the code compile? (Choose all that apply)



A. import aquarium.*;
B. import aquarium.*.Jelly;
C. import aquarium.jellies.Jelly;
D. import aquarium.jellies.*;
E. import aquarium.jellies.Jelly.*;
F. None of these can make the code compile.

For those of you who are well studied on imports, does this code make sense enough that the answer is obvious? It seems to me that without knowing what's in the packages I can't tell which answers are correct. There are similar questions for packages, also. And those questions were really the only ones I got wrong. So do I need to study packages more or does this question seem confusing to you as well?

Thank you !!

 
Enthuware Software Support
Posts: 4318
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Travis Roberts wrote:
For those of you who are well studied on imports, does this code make sense enough that the answer is obvious?


Yes, it makes sense. It is actually so easy that if I tell you the answer, you will kick yourself It is based on a very simple rule about package and import statements. I am sure any good certification book will explain in the relevant chapter.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

I re-read the sections on imports/packages. What still confuses me is why A is an incorrect answer?

Shouldn't this code

import everything underneath it?

Which is one thing that helped me get confused. Shouldn't the first import include all the others?
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, "import aquarium.*" does indeed import everything below it... which means everything in the "aquarium" package. It looks like you think that aquarium.jellies.Jelly is in the "aquarium" package, but it isn't. It's in the "aquarium.jellies" package, which is not part of the "aquarium" package. So your concept of "below" is just slightly off, compared to how things actually work in Java. Does that clarify the question for you?
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, yes, "import aquarium.*" does indeed import everything below it... which means everything in the "aquarium" package. It looks like you think that aquarium.jellies.Jelly is in the "aquarium" package, but it isn't. It's in the "aquarium.jellies" package, which is not part of the "aquarium" package. So your concept of "below" is just slightly off, compared to how things actually work in Java. Does that clarify the question for you?



Yes, it does. But I thought packages had to be the first line of code here. Here packages are also on lines 3, and 5. It's making me get confused between packages and imports.
 
Travis Roberts
Ranch Hand
Posts: 54
C++ Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see the problem I was having now. The authors of the book are just trying to prepare me for the exam. Each package is followed by its public class. Then the final package needs the import statement. I was used to only seeing one package statement because I've only seen one package statement per file... but here they crammed it all in I guess for brevity reasons.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!