Learn Java Programming
5.0 Packages
The following are the objectives of this module:
- Understand why we need packages
- How to define and use packages in Java
The Background:
It is common for two people to have the same name. You must have met at least once in someone in your life time, who has the same name as you do.
Well, even though if you have met someone with the same name as you have, you know that you both are not the same – there are some distinguishable facts – you know you are as unique as the person you’ve met.
Maybe you have a unique UUID number, or SSN or a unique passport number.
Okay, what does this have to do with software programming, and that to, Java programming.
It is very likely that you have named a class, the same name that I have used in my programs. And it is highly likely that we as a programming community will keep using the same class names again and again.
But how do we maintain uniqueness in our class names – how do I distinguish my class from yours, when we both have used the same name
Well, Why is the uniqueness required, first of all, you may ask – it is for a reason!
Imagine for a moment you are an established and successful software company already. Lets call it YOU Inc.
Now there is your friend, is also a promising company, starting out. Lets call it THEY Inc.
Now let us assume I am a freelance software programmer. And some company has contracted me to write software for them.
OK, so good so far!
But the challenge this company that I am working for, has installed software from both YOU Inc, and THEY Inc. And I am supposed to use both the libraries and write new code.
The strange irony is that YOU Inc, has a class called Student and THEY Inc. also has a class called Student.
When I initialize a Student object in my code, does it belong to YOU Inc. library or does it belong to THEY Inc. library, I am confused and so is the poor Java compiler.
This, if you would notice, is a classic example of name-space collision. Programmers having to deal with classes defined in the same name.
Somehow we need a mechanism to identify the Student in YOU Inc. library, separately from the Student in THEY Inc library. If only we could have a UUID or a SSN?!
Well, the CLSID naming convention employed originally by Microsoft used a 128 unique number that would be unique in time and space – it served the purpose of uniquely identifying the class with the same names, but it came with a price. It was hard to read and maintain.
We needed something UNIQUE to identify the classes.
Well, WAIT! , your company YOU Inc, has a website, doesn’t it? You are quite proud of it, isn’t that so? It obviously is www.youinc.com
Well, THEY Inc, also have a website www.theyinc.com
Can I not use this Unique Identifier to identify the Student classes separately?
Well, that is the solution the founders of the language envisioned.
Take your company website address – and turn it around and drop the www
Its going to be com.youinc
Well, add that prefix to your class:
com.youinc.Student
No! make it better still, add the name of your software package as well
com.youinc.collegeManager.Student
And THEY Inc, would have something like this
com.theyinc.accountsManger.Student
Now we have struck upon a unique naming convention for our classes
This is called Packaging!
The Student class is now packaged under
com
|
|___ youinc
|
|
|___ collegeManager
|
|___ Student
Now your Student.java class belongs to the package com.youinc.collegeManager
To use packages in Java we need to do two things:
- Add the package declaration as the first line of code in the source code
- Use nested folders to represent the package hierarchy
For example:
package com.youinc.collegeManager;
public class Student {
. . .
}
The Student.java file should be reside under the sub folders under
the main source directory with the path given as
com/youinc/collegeManager
- A Java Package is naming mechanism to uniquely identify a class across other similar implementations.
- A Java Package corresponds to a nested folder structure that is compiled as path information by the Java compiler.
- Packages are an easy solution to the classical class name collision in Software Engineering.
Summary:
- Packages solve the classic problem of name space collision is an simple and effective manner
- Java packages correspond to nested folder structures – the class file has the package name as the topmost line of code