Ocp Packages And Imports
OCP - Packages and imports
In Java, packages are used to group classes. This is to prevent naming conflicts and to better organise the code.
There are two types of packages:
- Built-in packages (packages in the Java API that come with the JDK)
- User-defined packages (custom-build packages, not part of the Java API)
All classes belong to a single package. The package a class belongs to can be specified explicitly as the first statement in a source file, or by not specifying a package, in which case all top-level types defined in a source file implicitly belong to the default (unnamed) package.
The default package is not actually a package called default; there is no package called default; we just refer to the unnamed packaged as such.
Because the default package has no name, we cannot refer to it in source files. So we cannot do import *
.
The typical naming convension for packages is the reverse domain name format, for example: com.example.rewards.SomethingCool
, where SomethingCool
is the name of the source file that resides in the rewards
package.
Importing packages, classes, and members
To use the functionality declared in a class that resides in a separate package we must import the class using the import statement, for example: com.example.rewards.SomethingCool
. This uses the Fully Qualified Class Name (FQCN) format.
If you declared a type using the simple class name, e.g: SomethingCool test = new SomethingCool()
without specifying the FQCN, the compiler will assume you are referring to a class that exists in the same package. If the class does not exist in the current package the compiler will generate an error if there is no import matching the declared type.
Import doesn’t actually import anything
The import
statement is a misnomer as nothing is actually being imported. This statement is actually telling the compiler which package the declared class resides in, only. If the compiler can not find the class in the “imported” packages, then it will generate an error.
Do not use wildcards
It is possible to use a wildcard when importing a package, for example: com.example.rewards.*
but this is considered bad practice, and can lead to complications if there are classes with the same names in different packages. Only import the specific classes that you intend to use.
Importing static members
Since Java 5, it is possible to import static members of a class using the wildcard symbol (*
) or by specifying the member specifically. For example import static com.example.reward.SomeUtils.*
will import all of the static fields and methods in the SomeUtils
class (but not the class itself), and import static com.example.reward.SomeUtils.aMethod
will import just the single static
method.
What you cannot import
- You cannot import subpackages. All packages must be imported separately, even if one package resides within another.
- You cannot import a package, class, or member that does not exist.
- Classes without a declared package (or classes in the default package) cannot be imported in other classes.
- If there are two classes with the same name defined in two separate packages, then the simple class name cannot be used as this would be ambiguous and the compiler will not know which class we intend to use in the type declaration. In this case the FQCN still has to be used.
java.lang
This is a built-in package that is always imported in all classes.