Sunday 7 July 2019

Packages in java - Online Help

Packages in java:

Java provides a mechanism for partitioning the class name into more manageable chunks. This mechanism called the "package in java".

A package in java can be defined as a group of similar types of classes, interfaces, enumeration, and sub-package. Using package it becomes easier to locate the related class. The package is both naming and a visibility control a mechanism.

You can find a class inside a package that is not accessed by code outside that package.

You can also define a class member that is only exposed to another member of the same package. This allows your classes to have intimate knowledge of each other, but no expose that knowledge to the rest of the world.

Types of packages in java:

There are two types of packages are available in java. They are:
-System package/Java API package
-User-defined package

System package/Java API package in java:

System package is available in the java system. The various system package is as follow:

java.lang-language package:
This package contains language support classes.It includes classes like string,math,thread,object,exception-handling,system etc.

java.util-utilize package:
This package contains language utility classes.It includes classes like random,date,vector,string tokenizer,data structure etc.

java.io- input output package:
This package contains classes which support input/output of data.it include classes like inputStream, outputStream,file etc.

java.awt-GUI Package:
This package contains classes which are used for implementing graphical user interface(GUI) in java.It include classes like font,layout manager,graphical classes such as button, textArea, checkBox, radioButton, textField,List,menus etc.

java.net-networking package:
This package contains classes are used for networking. It includes classes like url,inetaddress, sockets, etc.

java.applet-applet package:
This package contains classes for creating and implementing package. Most important classes in this package are applet.

java.sql-Database package:
This package contains classes for implementing database connectivity. The classes in this package are used for connecting to the database and performing operations on the database

User-defined Package in java:

Defining Package:

Creation of packages is very easy: simply include a package command as the first statement in the java source file. Any classes declared within that file will belong to the specified package.

Package statement defines the name of the package in which classes are stored. If you omit the package statement, the class names are put into the default package, which has no name.

While the default package is good for a short and simple program. But, most of the time, you will define a package, for your code.

The general form of the package statement is,

Package pkg;

Here,

The package is the java keyword, and pkg is the name of the package.

For example,

Package mypackage;

The following statement creates a package called mypackage.

Java uses file system directories to store package. For example, The class files for any classes you declared to be the part mypackage must be stored in the directory called "mypackage".Remember that, the directory name must match the package name exactly.

More than one file can be included in the same package statement. The package statement simply specifies to which package the class file is defined.


Hierarchy of package/Multilevel package:

You can create a hierarchy of package to do so, simply separate each package name from one above it by using the period symbol. The general form of a multilevel package is,

Package pkg1[.pkg2[.pkg3]];

This hierarchy must be reflected in the file system of our Java development system. For example,

Package java.awt.image;

It will store in java/awt/image or java\awt\image or java:awt:image on you Unix, window or Macintosh file system respectively.

Choose your package name carefully because you cannot rename a package without renaming directory in which the class is stored.

CLASSPATH:

CLASSPATH is an environment variable that determines where the JDK tools such as Java compiler and an interpreter Search for a class file. Here, are some tips for setting the CLASSPATH environment variable UNIX/LINUX and WINDOWS.

-On UNIX/LINUX, edit your shells startup file. If you use the c shell, add a line such as the following to the .csrc file in your home directory.

setenv CLASSPATH/HOME/USER/CLASSDIR;.

 If you use the Bourne again shell or bash, add the following line to the bashrc or bash-profile file in your home directory.

export CLASSPATH=/home/user/classdir;

-On Windows 95/98/ME, edit the auto exec.bat file in the boot drive. Add a line

SET CLASSPATH=c:\user\classdir;

-On Windows NT/2000/XP, select my computer icon in your desktop and right click the mouse, choose properties option. The system properties dialog will appear.
then click the new button, add a new environment variable named CLASSPATH, or edit the variable if exist already.



Importing the Package in java:

Java includes the import statement to bring the certain classes, or the entire package, into visibility. The import statement is a convenience to the programmer and is it technically needed to write a java program. If you are going to refer to a few classes in your application.however, the import statement will save a lot of typing.

In java programming, the import statement is declared first before the declaration of any class definition. The general form to import statement is:

import pkg1[.pkg2].(classname | *);

Here,

pkg1 is the name of the top-level package and pkg2 is a subordinate package inside the outer package hierarchy. Finally, you can specify either an explicit classname or (*), which indicates that the java compiler should import the entire package. The below codes shows both forms in use:

import java.util.Data;

import java.io.*;

All the standard Java classes included with Java is stored in a package called java. The basic language function is stored inside the java package called"java.lang".Normally, you have to import every package or class that you want to use, but since Java is useless without java.lang, it is implicitly imported by the compiler for all the programs.

import java.lang.*;

If a class with the same name exists in two different packages that you import using the star form. In that case, you will get a compile-time error and have to explicitly name the class specifying its package. when you use a class name, you can use its fully qualified name, which includes full package hierarchy. for example,

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

The same example without the import statement looks like this:

class mydate extends java.util.Date
{
}


Advantages of packages in java:

-Packages are useful to arrange related classes and interfaces into a group. This makes all the classes and interfaces performing the same task to put together in the same package. For example, in Java, all the classes and interfaces which perform input and output operations are stored in the java.io.package.

-Packages hide the classes and the interfaces in the separate subdirectory, so, that accidental deletion of classes and interfaces will not takes place.

-The classes and interfaces of a package are isolated from the classes and the interfaces of the other package. This means, that we can use the same names for classes of two different classes. For example, there is a data class in "java. util" package and also there is another data class in "java.sql" package.

-A group of a package called a library. The classes and the interfaces of a package are likes books in a library and can be reused several times. This reusability nature of packages makes programming easy. Just Think, the packages in Java are created by java soft people only once and millions of programmer all over the world are daily by using them in various programs.

No comments:

Post a Comment