Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Why do we need PACKAGES in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do we need PACKAGES in java ?" Watch "Why do we need PACKAGES in java ?" New topic
Author

Why do we need PACKAGES in java ?

java tech
Greenhorn

Joined: Jul 13, 2005
Posts: 4
In C & C++ the name of each class or function was taken from the same namespace. This means a unique name has to be used for each class name to avoid class name collision. But using this procedure , after a while we run out of convenient, descriptive names for individual classes. We must assure that the name we choose should be unique and not collide with class names chosen by other programmers. To solve this problem Java provides a mechanism for partitioning the class namespace into manageable chunks. This mechanism is the PACKAGE. Packages are containers for classes that are used to keep the class name space compartmentalized. The package is both a naming and a visibility control mechanism. We can define classes inside a package that are not accessed by the code outside that package. This allows our classes to have intimate knowledge to the rest of the world.
In general , a Java source file can contain any of the following four internal parts:
�A single package statement (optional).
�Any number of import statements (optional).
�A single public class declaration (required).
�Any number of classes private to the package (optional).

To create a package is quite easy: we just have to include a package command as the first statement in a java source file. Any classes declared within that file will belong to the specified package. The package statement defines a namespace in which classes are stored . If we omit the package statement ,the class names are put into the default package, which has no name.

package Mypack;

Java uses file system directories to store packages. For example , the .class files for any classes is to be declared a part of Mypack must be stored in a directory called Mypack
More than one file can include the same package statement.
We can also create a hierarchy of packages. To do so we simply separate package name by using a period.

package Mypack.pk1;

We cannot rename a package without renaming the directory in which the classes are stored.
A simple question arises in our mind ,how does the Java run-time system know where to look for packages that we create. First , by default , the Java run-time system uses the current working directory as its starting point. Thus if our package is in the current directory ,or a subdirectory of the current directory ,it will be found. Second ,we can specify a directory paths by setting the CLASSPATH environmental variable.
For example in order for a program to find Mypack, one of two things must be true. Either the program is executed from a directory immediately above Mypack, or CLASSPATH must be set to include the path to Mypack.
Java also include import statement to bring certain classes,or entire packages, into visibility. Once imported ,a class can be referred to directly by using its name.

import pkg1.pkg2.*;

for example we can import using the following statement

import java.util.*;
class MyDate extends Date{
}

Packages are used extensively while writing programs in java so that we can use the same name for referring to different programs & there is no namespace collision.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,

Welcome to JavaRanch!

In your haste to come in and share your knowledge, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- no joke names, "handles," or last initials are acceptable. We take this rule quite seriously.You can fix your display name here. Thanks for your cooperation!

Further, the Big Moose Saloon is a place to ask questions and get answers, not a place to host your static content. If you'd like to post essays about Java programming, I suggest you sign up at one of the myriad blog hosting services -- or you could consider writing an article for the JavaRanch Journal. Otherwise, you're certainly welcome to hang around and answer questions.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hmm. This is apparently your second warning.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
I'm guessing he never comes back after posting and therefore never see's the warnings. Why someone would come on here just to post something like that and never check back I can't explain.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Oh, dear.

Well, thanks for sharing. o_O
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by java tech:
In C & C++ the name of each class or function was taken from the same namespace. This means a unique name has to be used for each class name to avoid class name collision.


Or you simply use a namespace: http://www.cplusplus.com/doc/tutorial/tut5-2.html

:roll:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do we need PACKAGES in java ?