Packages
Instead of thinking it at java level,consider it as simple English word and understand its meaning.
Consider a situation from your daily life, Whenever we have to relocate somewhere, we take cartons and pack our related stuffs in different cartons, we call it "packaging" and this combination of related stuff in one carton make it a "package".
Now,consider why we package different related stuffs in different cartons?
Answer is simple,it is just process of organizing the stuff.And more whenever we need to use it or unpack it, we should not confuse or conflicted and can access those packed stuffs in easy way.
Same way in Java,
- A Package is a grouping of related types.Here type refers to classes, interfaces, enumerations, and annotation types.
- The types that are part of the Java platform are members of various packages that bundle classes by function. for example :fundamental classes are in java.lang
, classes for reading and writing (input and output) are in
java.io, so on...many different packages are available in java, based on functions they perform.
When we create an application in java, then writing the classes in proper packages is considered important factor
When we use package in our .java file then package should be first statement in your java source file
Specifying class name with package name called as full-qualified name for the class.
Advantage of packages Packages avoids name conflicts It resolves naming conflict of classes by prefixing the class name with a package name. For example
,in.technoscience.Abc
and in.science.Abc
are two distinct classes. Although they share the same class name Abc
, but they belong to two different packages:
in.technoscience
and
in.tscience
. These two classes can be used in the same program and distinguished using the fully-qualified class name. This mechanism is called Namespace Management.Packages provides hierarchical organization
With packages, you can organize a program into logically related groups of types, and organize the groups hierarchically. For example, all the types in a particular family of types could belong to the same package, or be spread out across several packages.
A class in one package can subclass a class in another package. The only requirement is that the subclass must specify the name of the package containing its superclass as well as the superclass's simple name. When you organize your types into packages, what you are actually organizing is type names.(i will explain in upcoming post).
Packages helps in Access Control
Java has access control modifiers public,private,
protected
and default,out of which protected and default are related to package. A protected entity is
accessible by classes in the same package and its subclasses. An entity
without access control modifier (i.e., default) is accessible by
classes in the same package only.(i will explain these all in upcoming posts)Packages helps in building Libraries for distribution
Any Java program you write will make use of libraries developed by others and made available to your program as packages. Any program will at least use the run-time libraries of the Java API, some of which are java.lang,java.io
,java.util
java.applet and many more. If, rather than developing a
complete program, you wish to develop class library that other developers can use in their programs, your
end-product will be a package.
Package Naming Conventions
Package names are written in all lower case to avoid conflict with the names of classes or interfaces.
A package name is made up of the reverse of the Internet Domain Name (to ensure uniqueness) plus your own organization's internal project name, separated by dots
'.'
Packages in the Java language itself begin with java or javax
Package Name and Directory Structure
The package name is associated with the directory structure used to store the classes. The classes (and other entities) belonging to a specific package are stored together in the same directory. Furthermore, they are stored in a sub-directory structure specified by its package name.
For example, the class
Abc
of package in.technoscience.first.subproject2
is stored as "$BASE_DIR\in\technoscience\project1\subproject2\Abc.class
", where $BASE_DIR
denotes the base directory of the package. The "dot" in the
package name corresponds to a sub-directory of the file system.The base directory (
$BASE_DIR
) could be located anywhere in the file system. Hence, the Java compiler and runtime must be informed about the location of the $BASE_DIR
so as to locate the classes. This is accomplished by an environment variable called CLASSPATH
. CLASSPATH
is similar to another environment variable PATH
, which is used by the command shell to search for the executable programs.
No comments:
Post a Comment