The best source to learn about packages is the Java Tutorials:Creating and Using Packages (The Java Tutorials)
Consult the above link to study everything about packages in Java. In this tutorial, I focus on emphasizing the key and important points, especially how to run Java files which are under a package using the java command.- com.mycompany.model: contains entity classes.
- com.mycompany.business: contain business classes.
- com.mycompany.gui: contains GUI classes.
etc.
package com.nam.pets;
public class Dog {
}Note that the packagestatement must be the only one and placed at the beginning of the source file. In case there are multiple classes in a source file, all classes are under the declared package.- Second, create a directories structure that follow the package name from right to left. For the package com.nam.pets, we have to create 3 nested directories like this:
NOTE: If you are using an IDE such as NetBeans or Eclipse, the IDE creates the package directory structure automatically for you. But you should understand and know how o create packages manually when needed. How to compile a Java file under a package using the javac tool?Just like normal: javac Dog.java How to run a Java file under a package using the java tool?This requires an extra step as compared with files that do not under any packages.- First, change the current directory to the parent directory of the first directory in the package directories structure. For example, if the package name is com.nam.pets, change the current directory to the parent directory of the comdirectory.- Second, specify the fully qualified name of the class with the java command. For example:java com.nam.pets.DogNOTE: When using an IDE, the IDE does this task behind the scene to save time. But you should understand the principle so you will be able to run a Java file under a package manually when needed.What if we want to use the Dog class from another class which is in another package, e.g. the Trainerclass in com.nam.training package?In this case, there are two ways:First, using the fully qualified name of the referenced class. For example:
package com.nam.training;
public class Trainer {
void teach(com.nam.pets.Dog dog) {
dog.bark();
}
} This sounds okay. But if the Dog class is used repetitively, then using the fully qualified name makes the code hard to read. Hence we have the second way below. import com.nam.pets.Dog;This refers to the Dog class in the package com.nam.pets.The import statements should be placed after the package declaration and before the class declaration. For example:
package com.nam.training;
import com.nam.pets.Dog;
public class Trainer {
void teach(Dog dog) {
dog.bark();
}
}If the two classes are in the same package, there’s no need to use the import statements. There are two cases in which Java does the importing implicitly for us:import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import java.util.Set;Here, the wildcard character * makes our life easier, as we can shorten the above import statements into just one import statement like this:
import java.util.*;NOTE: The wildcard imports do not include types in sub packages. That means java.util.* does not include types in the java.util.concurrent package. Thus we have to import the sub packages explicitly:
import java.util.*; import java.util.concurrent.*; import java.awt.*; import java.awt.event.*;
int degree = 38; double tetha = Math.PI * Math.sin(degree) + 3 * Math.cos(degree);With a static import:
import static java.lang.Math.*;Then our code would look more succinct:
int degree = 38; double tetha = PI * sin(degree) + 3 * cos(degree);The latter looks better, right?Like regular import statements, we can use the static imports to refer to a single type or a group of types using the wildcard character *. For example:
import static java.lang.Math.PI; import static java.lang.System.*;That’s the core stuff about import statements in Java. I recommend you to read the Java Tutorials for more details: Using Package MembersAnd here I shared my practical experiences with regard to import statements:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.