A package is a collection of Java files and classes. Packages
help you keep all your files organized by allowing you to place files that you
want to associate with one another together. Also, packages help you to avoid
class name clashes. This means that you can have two classes with the exact
same name but the package ensures that Java knows which class you are referring
to.
You can specify the package to which your class belongs to by
filling out the package field as you create your class. Even if you create a
class without filling out the package field, you can later add it to the
package you like by adding a statement that shows the package to which the
class belongs. The statement specifying the package should be the first line of
the Java class file. Let us assume that we want the class to belong to the
package named “boats.” In the class file, you can add the following line at the
very top of the class file, even before the declaration of the class like this:
package boats;
However, there are a couple of things that you should know
about packages and the naming conventions that accompany them.
First, the name of any package should be in lowercase letters
without any underscores or special characters.
Second, the fact that somewhere in the world, someone might
give their package the same name as yours, hence causing name conflicts
necessitates a way to make packages universally unique. This is especially if
you intend to distribute your classes so that other people can use them.
To make a package name unique, you should write the domain
name of your website in reverse order. Then, add a dot and the name of the
package. So let’s say you have a website whose URL is www.codingjourneydiary.com, and you
want to name your package “boats”, your package name could look something like
this:
com.codingjourneydiary.boats
So why do we use domain names? Domain names are a great
solution because they are unique. There can’t be two identical domain names
because it would create confusion as to which website is being referred to.
So, whenever anyone wants to use your class in their program,
they can start their class files by adding an import statement like this.
Assume that the name of the class is “yatch”:
import com.codingjourneydiary.boats.yatch;
However, assume that you have several classes in your package
such as Yatch, Kayak, RacingBoat and Canoe. If you want to use these classes
in your programs, you will have to add import statements as follows:
import com.codingjourneydiary.boats.yatch;
import com.codingjourneydiary.boats.kayak;
import com.codingjourneydiary.boats.racingboat;
import com.codingjourneydiary.boats.canoe;
Notice that you can import all the classes in the package
like this:
import com.codingjourneydiary.boats.*;
The * in the above statement is called the wildcard and what
it does is it tells Java to add all the classes in the package
com.codingjourneydiary.boats. This is a better way to add imports because your
code remains organized and you avoid annoying warnings that you could have
avoided by importing all the classes in the package.
Notice also that Java also checks whether you have used the
packages that you have imported. So if you declared an import statement but
never used the package in any way, Java will suggest that you remove the unused
imports. Besides, modern IDEs allow you to fix or organize imports, and what
this means is that it helps you add all the necessary imports, while removing
the unused ones.
That’s all on packages for now. In the next tutorial, we are
going to discuss how to read text files in Java.
In case you have any questions or comments, please drop them
in the comments section below and I will address them.
No comments:
Post a Comment