Java packages are a simply a way to manage our Class files. They are really helpful in make everything neat and easy to find.
People always try to have everything in an organized manner. This is valid to Java as well. Actually, why do we need that? Why do we want to organize everything in our way? Simply, it’s because, when we do so, we can analyse them, study them and sort out things easily.
Example from the Real World
For example, think of animal kingdom. We basically divide all the animals in the world in to two categories. Vertebrates(animals with a backbone) and invertebrates(without a backbone). Each of these two again divides to many categories. Vertebrates divide to 5 major sub categories.
Each of these types will again divide in to their own sub categories. Similarly for invertebrates…
Just imagine, without dividing the animals like this, we would never ever be able to remember the features of each and every individual animal.
But if we see a fish, though we don’t know exactly what is its scientific name, we would know at least few basic features of it. It has gills to breathe, it can’t survive without water, it can swim, it is a cold blooded animal….etc. Similarly when we detect a new species, we can add its details in to its belonging category and once we need those info back, we know where to look at and it will save most of the time…
Applying in Java
Similar thing is happening in Java packages. They are nothing but a way to organize the Classes we write and prevent any collision between two Classes. They simplify our programming life.
Usually, we use packages to categorize Classes with the same functionality.
Java has a marvelous collection of toolbox like collection of Classes and we can get the support of them when writing our own Classes. These toolboxes are nothing but packages. For example, if we need a graphical user interface, we need a package called swing, which has in-built Classes for components like windows, text boxes, buttons etc…
Like this swing, there are many pre-built toolboxes are available for you to choose…
- java.util – Data structures collection
- java.io – Input output files operations
- java.net – Networking tasks
- java.sql – Database related
- java.awt – GUI programming
- javax.swing – GUI programming
- java.text – Handling text
There is a concept of sub-packages as well. For example, swing is actually a sub-package of the main, big package javax. Similarly, awt is a sub-package of java.
The following picture shows how java.util.Scanner Class an be broken down. Note that nextInt() is a method inside the Scanner Class.
As an example of working with toolboxes, check this out! JFrame is a Class of swing. If we create a new JFrame object in the main() method, we will get a blank window. It’s easy like that. But before that, we have to introduce JFrame to your working Class. In order to do that, use import statement.
The naming system is like this;
to import only one Class from a package.
Note that this statement is written before the Class declaration
OK. our JFrame import statement is written like;
As an alternative, you can import all the Classes in the swing package. For that use following format.
Now your working Class knows not only JFrame but all the Classes in the package swing such as JButton, JLabel, JPanel,JOptionPane…..etc.
If you need to import multiple Classes from the same package, you can use this way…this is often used in GUI programming.
Anyway, swing is not actually used today! The swing objects have an old look and feel…look and feel matters when developing desktop applications. Nobody likes to have a GUI in the 90s. Therefore we have GUI developing software platforms such as JavaFX today, to develop rich, modern looking user interfaces.
Back to the discussion. We can create our own packages as well. You might have seen that, after we create a new Class in a Java project in eclipse, it is already in a certain package called “default package”.
Default package is actually no package at all 🙂 We have to create our own packages when we are writing big programs with a lot of Classes.
Creating a package is simple. Go to File → new →Package
Make sure you have the Source folder as your working Java Project folder. Give your package a suitable name and hit OK.
Then, an empty package is created. Now you can add any Class file to it. Just drag a Class file from your default package to this newly created empty package. You can see that the Class file is then categorized under the new package.
The Classes in the same package can see each other. You don’t have to import them explicitly.
But Classes in different packages can’t see each other. Even if the two packages are in the same project folder. You have to import them.
Importing from the same Package
If you want to import any Class from another package in the same project, you can use the import statement in the form <PackageName>.<ClassName>;
If you want to import all the Classes of the package, use *.
How to import a Class from a different Project?
Now suppose you want to import a Class from a different project. For that, you have to do a bit more work 🙂
First, write click on the Java project folder that you are currently in and select Properties.
Then, go to Java Build Path.
From that, select, Projects and press “add” button in the right.
Next, select the Java Project folder where you want to import the Class from. Don’t forget to put the tick 🙂 Click OK.
Finally, you can call the import statement in the usual format <packageName>.<ClassName>; at the top of your Class.
One final note…
What happens if there are two Classes with the same name in your imports? For example, java.util.Date vs java.sql.Date?
Then when you create a Date object, your computer might fall in to a trouble of deciding which would be chosen.
So, it would be an error to import such two Classes with the same name and you have to avoid it. But if you really need objects of both of them, just refer the two Classes with their full names…
java.sql.Date date = new java.sql.Date(0);
java.util.Date date2 = new java.util.Date();