• 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

Need clarification for "import" keyword

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm preparing to take OCA exam in June. I'm reading OCA Java SE 8 Study Guide. There's one question I need clarification for "import" keyword. Exam number 7 for chap 1. Question is as below :

Given the following classes, which of the following snippets can be inserted in place of
INSERT IMPORTS HERE and have the code compile? (Choose all that apply)



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



Correct answers are : A,B,C

I understand why B & C are correct. However, I'm confused about being A is correct. There are two same class under different package. One inside "aquarium" package. Another one is "aquarium/jellies" package. importing using "import aquarium.*" doesn't make java to confuse which class from which package to import?

Thanks
 
Saloon Keeper
Posts: 10307
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Wildcards do not include sub-packages. When you use import aquarium.*; it imports all classes from the aquarium package, and none from the aquarium.jellies package.
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it doesn't import class from aquarium.jellies package, why A is correct
 
Stephan van Hulst
Saloon Keeper
Posts: 10307
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wouldn't it be? WaterFiller requires a Water class, and import aquarium.*; fulfills that requirement.
 
Claire Chan
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah i see. I thought I must import both classes.
thanks Stephan.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!