Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we need PACKAGES in java ?

 
java tech
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. This is apparently your second warning.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, dear.

Well, thanks for sharing. o_O
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic