1.4-1_Understanding Package Declarations and Imports Flashcards
How are grouped Java classes?
into packages
What does the import statement does?
The import statement tells the compiler which package to look in to find a class.
[WildCards] What is the wildcard symbol?
The * is the wildcard, that matches all classes in the package.
[WildCards] Does the wildcard slowdown your execution?
Including wildcard on import doesn’t slow down your program execution
Which package is automatically imported?
Everything in java.lang is automatically considered to be imported.
When is required to import packages?
✅ When referencing a class which is in another package. ✅ When you're using classes from different packages but have the same name. ⚠️Importing a class that is in the same package as the class importing it. (Is a redundancy)
[WildCards] Does a wildcard in a simple import matches also fields and methods?
import {package};
No, this is only achievable via static imports. With a simple import You cannot import methods only class names.
Could you have multiple wildcards in an import statement?
No, You can only have one wildcard and it must be at the end.
✅ import java.nio.;
❌ import java.nio..*;
[Naming Conflicts] What does Naming conflicts mean when importing classes?
When the class is found in multiple packages, Java gives you a compiler error.
[Naming Conflicts] Is there any precedence regarding wildcards and naming conflicts?
If you explicitly import a class name, it takes precedence over any wildcards import. import java.util.*; import java.sql.Date; java.sql.Date will take precedence in these imports.
What options do you have when you need to use 2 classes with the same name?
o You can pick one to use in the import and use the other’s fully qualified class name. o Or you could have neither with an import and always use the fully qualified class name.