The packages are used to organize related classes and interfaces together into groups.
For instance, take a look at the java API and you will see that all the classes related to IO are in the java.io package and the classes related to networking are in the java.net package.
Packages can be nested, that's to say, a package can contain other packages. For instance, you can see that all Collection classes are in the java.util package, but all concurrency related Collection classes are in are in the java.util.concurrent package.
A great adventage of using packages is the encapsulation. You can hide classes in a particular package so that those classes just can be used for another classes in the same package and no one else can instantiate them or extend them. That is by means of the accessibility mechanism provided by the Java Programming Language.
One more advantage is that when you compile the code, tha java compiler will create a directory structure resembling your package structure. That gives us the possibility to have two classes with the same name as long as they belong to different packages. So our packages let us take control of our namespace.
Packages in java are used for good organization and name scoping.
Imagine a situation where many programmers working in different parts of the project write the their own java file with the same name . If all these are included in the classpath and if these classes are not explicitely in the packages then there is no way we can tell JVM , which particular class we are reffering to.
there are lots of advantages using packages.. i will let you know wat comes to my mind right now.
1. moduling. that is u can module up your group of classes into a package and subpackages according to the functionality which they give.
2. accesibility. using packages you can define the accibility of your code using the access modifier like public, protected, default etc.
3. name collision. this is very important. the main thing is you cant expect your co programmer to name a method or class other than wat u name to your classes and methods. if same name has been given to two methods of different packaged classes still java packaging makes you use both without any name collision problem
you could note that both are Date class. but still java allows you to make use of both. there are some more uses also there. but right now i am able to recall only this. probably some other ranchers could help u better.
A package is basically just a way of logically grouping classes together. Just take a look at the Java SDK. There are tons of classes there. Having them in packages makes it a little easier to find what you are looking for. Packages also provide a way of having qualifiers. So I can have a class named java.io.File and a class named barry.io.File running in the same JVM.
Joined: Jan 24, 2006
sweet, thx for the posts.... I figured is was used to clean up applications
Uncle Bob Martin talks about principles of package design and gives some neat ideas about how they might be organized.
You should probably think "component" instead of a literal Java package for most of these. Java packages are on the small side for build or deployment units. But you can manage dependencies in real Java packages per all his guidelines to great benefit.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi