Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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: 12165
258
  • 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: 12165
258
  • 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.
 
Dinner will be steamed monkey heads with a side of tiny ads.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic