It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose there is already a class named Rectangle in java.awt package, and we want to name a new class Rectangle in the graphics package. Although they are in distinct packages, the compiler permits both classes to share the same name. Each Rectangle class's fully qualified name contains the package name. That is, the Rectangle class in the graphics package has completely qualified name graphics.Rectangle, while the Rectangle class is in java.awt package has the fully qualified name java.awt.Rectangle. This works fine unless two separate programmers use the same name for their packages. This problem is prevented by convention.
Package names are written in all lowercase to prevent confusion with the names of classes or interfaces.
Companies utilize their Internet domain name in reverse to start their package names, such as com.example.mypackage for a package written by a programmer at example.com with the name mypackage.
Within a single business, name clashes must be handled according to the convention. For example, the area or project name may be added after the company name (for example, com.example.region.mypackage).
The Java language itself has packages that start with java or javax.
The internet domain name might not always be a legal package name. This can happen if the package name starts with a character prohibited from being used as the first character in a Java name, such as a digit or the special character "int," or if the domain name contains a hyphen or another special character. The recommended practice in this situation is to include an underscore.
Legalizing Package Names
|Domain Name||Package Name Prefix|
Using Package members
Package members are the several categories that make up a package.
You must perform one of the following actions to use a public package member from outside its package:
• Identify the member by their full legal name.
• Import, the package member
• Import, the whole package of the member
The sections that follow show how each is suitable for various circumstances.
Referring to a Package Member by Its Qualified Name
Since the beginning of this tutorial, most examples have referred to types by their brief names, like Rectangle and StackOfInts. If the code you are creating is in the same package as a package member or if the member has been imported, you can use the member's simple name.
However, you must use the member's fully qualified name, which incorporates the package name, if you are attempting to utilize a member from a separate package that has not yet been imported. The Rectangle class declared in the graphics package in the preceding example has the following fully qualified name.
You could use this qualified name to create an instance of
graphics.Rectangle myRect = new graphics.Rectangle();
For occasional use, qualified names are acceptable. However, if a name is typed repeatedly, it becomes tiresome, making the code more challenging to decipher. You can also import the member or its package and then use its short name as an alternative.
Importing a Package Member
Place an import statement at the beginning of the file, after the package statement (if present), but before any type declarations to import a specific member into the current file. Following is a description of importing the Rectangle class from the graphics package developed in the previous section.
The Rectangle class can now be referred to by its short name.
Rectangle myRectangle = new Rectangle();
This strategy works nicely if you only use a few members of the graphics package. However, you should import the whole package if you use many types from it.
Importing an Entire Package
Use the import statement with the asterisk (*) wildcard character to import every type found in a specific package.
Any class or interface in the graphics package can now be referred to by its short name.
Circle myCircle = new Circle(); Rectangle myRectangle = new Rectangle();
The asterisk in the import statement can only be used to specify every class in a package, as in this example. It cannot be used to match a portion of a package's classes. The following, for instance, does not correspond to all classes in the graphics package that start with A.
// does not work import graphics.A*;
Instead, a compiler error is produced. Using the import statement, you typically import just a single package member or a whole package.
Note: You can import the public nested classes of an enclosing class using a different, less frequent type of import. As an illustration, consider the case where the "graphics.Rectangle" class had valuable nested classes like "Rectangle.DoubleWide" and "Rectangle.Square”.
The following two statements could be used to import Rectangle and its nested classes.
import graphics.Rectangle; import graphics.Rectangle.*;
Note: the second import statement won't import Rectangle, So keep that in mind.