File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes package? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package?" Watch "package?" New topic


Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
what is the purpose of a package?

and what benefits are there for using this?

thanks for the posts..


You down with OOP? Yeah you know me!
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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 package and the classes related to networking are in the 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.

I hope this helps.

Edwin Dalorzo.
Suchitra Bhat
Ranch Hand

Joined: Dec 30, 2005
Posts: 62

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.

Hope this clears your doubt

vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
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

eg: java.util.Date; java.sql.Date

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.

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

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 and a class named running in the same JVM.
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
sweet, thx for the posts.... I figured is was used to clean up applications

and code..

thx again..

Monk Justin Fox.....
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
In the simplest word: the function of package is just the same the function of folder. You don't want save your 200 mixed files in one folder, did you?

<a href="" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: package?
It's not a secret anymore!