Java 11 Developer Certification - Packages and imports
September 21, 2020
What we are covering in this lesson
- How to create java packages - the package keyword
- How to use classes in packages - the import keyword
How to create java packages, the import statement
As per the original Oracle java documentation:
Definition: A package is a grouping of related types providing access protection and name space management.
In other words packages provide us with better code organization (think of a tree structure, like your direcory setup on your computer).
- Use the package keyword to indicate in the java source code that the class is part of a package.
- A java source file can contain 0 or more package statements
- if we use the package keyword, it must be the first statement excluding whitespace and/or comments
- a class can only be part of a single package, but a package can contain multiple classes
- the package hierachy is delimited by . e.g.: package vehicle.motorbike.ducati;
Let’s see a sample usage of the java package statement through an example. As described previously, the java packages are actually derived to a folder structure, so in case we have class defined as:
package vehicle.motorbike.ducati;
public class Streetfighter {
public static void main(String[] args) {
System.out.println("V2 is king");
}
}
then the actual java source of the above should be in the following directory (of course, creating and managing packages are much easier using an IDE):
darvat@tamas-OMEN:~/IdeaProjects/HelloJava/src$ tree ./
./
└── vehicle
└── motorbike
└── ducati
└── Streetfighter.java
3 directories, 1 file
How can we compile this class? The following example will show us just that:
darvat@tamas-OMEN:~/IdeaProjects/HelloJava$ pwd
/home/darvat/IdeaProjects/HelloJava
darvat@tamas-OMEN:~/IdeaProjects/HelloJava$ javac -d out/ src/vehicle/motorbike/ducati/Streetfighter.java
darvat@tamas-OMEN:~/IdeaProjects/HelloJava$ tree ./out/
./out/
└── vehicle
└── motorbike
└── ducati
└── Streetfighter.class
3 directories, 1 file
Note that we provided the FQCN (Fully Qualified Class Name) after javac, we also defined a specific output directory for the compiled class (used the -d option).
The result (compiled class) appears in a similar directory structure as we have for the source (of course, under the given output directory -d out).
Now that we have a compiled class, let’s run it:
darvat@tamas-OMEN:~/IdeaProjects/HelloJava$ java -cp out/ vehicle.motorbike.ducati.Streetfighter
V2 is king
After the java executable we provided the root folder of the compiled class (./out), then we used the FQCN to run it.
How to use classes in packages - the import keyword
There are 2 ways of using or referencing a class that resides in a package
- using a FQCN, this is only adviseable when we use a class once or twice in our code, otherwise it will very quickly get bloated
- import them to our source
Let us look at samples for both of the cases and start with the FQCN option:
package vehicle.motorbike.ducati;
public class Streetfighter {
public static void main(String[] args) {
System.out.println("V2 is king");
System.out.println("Go for a big one, over " +
java.lang.Math.sqrt(1000000) +
"ccm");
}
}
Check out how we referenced the sqrt method, we provided the FQCN (java.lang.Math.sqrt).
Now we should focus on the more convenient and most common way of achieving the same, using the import keyword:
package vehicle.motorbike.ducati;
import java.util.Date;
public class Streetfighter {
public static void main(String[] args) {
System.out.println("V2 is king");
System.out.println("Go for a big one, buy one today: "
+ new Date());
}
}
Here we imported the Date class from java.util and used it without FQCN.
Also, there is another way for importing classes, more precisely importing all classes from a given package by using the *. If we stick to the previous example of using the Date class, we could write import java.util.*, but this is not that elegant and descriptive as properly and precisely importing only the required classes we want to use. So, do not use a star import and your colleagues will thank you for that. Modern IDEs will also do it for you, do not be lazy
How about importing static members and static variables of classes from another package? Can we import them? Absolutely, here is an example:
// importing a single static class member
import static java.lang.Math.sqrt;
//importing all static members and variables of a class
import static java.lang.Math.*;
public class Streetfighter {
public static void main(String args[]) {
// use of java.lang.Math.PI static variable
System.out.println("The value of PI : " + PI);
// use of java.lang.Math.sqrt static method
System.out.println("Square root of 100: " + sqrt(100));
}
}
IMPORTANT: single class import declaration will take precedence over star imports
It means that if you have a class in two different packages and you import the first with single class import and the second one with star import, then the first one will be used in your code when referenced without FQCN.
IMPORTANT: classes in the java.lang package are not required to be imported, java compiler takes care of it for us by default
That’s it for this lesson, happy learning