Java Lang Util Io Awt Net Appl Et
Java Lang Util Io Awt Net Appl Et
Introduction
Most programming languages provide ready made code libraries that contain a
group of related files. By using those contents of these libraries, we can
considerably save coding time and effort. The files under these code libraries are
sorted and arranged according to their functionality. In different languages they are
known by different names. In C they are called header files, in C++ class libraries
and in Java they are called Packages.
The main feature of OOP is its reusability. One way achieving this is by
extending the classes and implementing the interfaces. Another way of achieving
the reusability in Java is to use packages.
Advantages of packages
Java API provides a large number of classes grouped into different packages
according to functionality. The frequently used java API packages are
java
appl
lang util io awt net
et
java.lang
o This package contains utility classes such as vectors, date, random etc.
java.util
o This package contains input/output support classes. They provide
facilities for the input and output of date.
java.io
o This package contains input/output support classes. They provide
facilities for the input and output of date.
java.awt
o This package contains a set of classes for implementing graphical user
interface. They provide facilities for the input and output of date.
java.net
o This package contains classes for networking. They include classes for
communicating with local computers as well as with internet servers.
java.applet
o This package contains classes for creating and implementing applets.
This packages are organized in a hierarchial structure. There are two ways of
accessing the classes stored in a package. The first approach is to use the fully
qualified class name of the class that we want to use. This is done by using the
package name containing the class and then appending the class name to it using
the dot operator.
Naming conventions
Every package name must be unique to make the best use of packages.
Duplicate names will cause run-time errors.
Java language provides facility for creating our own packages. These
packages are called user-defined packages.
Creating packages
Method1
1) Declare the package at the beginning of a file using the package keyword
followed by a package name. This must be first statement in java source
file(except for comments)
Syntax: package my package;
Ex: siva
2) Import the required standard packages available using the import statement.
Ex: import java.util.*;
3) Declare and define the classes that will be included in the package. All the
members of the package should be public to be accessed from outside.
Ex: package mypackage;
Import java.util.*;
Public class Math
{
// class definition
}
4) Save the above definition in a java file and compile the classes defined in the
package. Compilation can be done with a -d option. This creates a folder
with the package name and places the .class file into the specified folder.
Javac d c:\java math.java
Method2
1) Create a subdirectory where the main source files are stored, the directory
name must match the package name exactly.
2) Store the listing as the classname.java file in the subdirectory created.
3) Compile the file. This creates .class file in the subdirectory.
Accessing a package
The import statement can be used to search a list of packages for a particular class.
Here
Import java.awt.*;
Where pack1 is the name of the user defined package when we import a package
using astrick(*) all public classes are imported. However we may refer to not
import certain outside of the package. Such classes should be declared not public.
CLASSPATH
While packages solve many problems from an access control and name space
collision prespective, they cause some curious difficulties. When you compile and
run programs. This is because the specific location that the java compilers will
consider as the root of any package hierarchy is controlled by CLASSPATH.
When java program is executed, the JVM searches for the classes used within
the program on the file system. It uses one of two elements to find a specific class.
The different access modifiers and its access locations as shown below while using
packages and inheritance in a program
Return(x+y);
Return(x-y);
Return(x*y);
Return(x/y);
//save as arith.java
Return(x>y?x:y);
Return(x<y?x:y);
//save as Stat.java
Import java.io.*;
System.out.println(addition :+a.add(10,20));
System.out.println(substraction:+a.sub(20,10));
System.out.println(multiplication+a.mul(10,20));
System.out.println(division:+a.div(20,10));
Stat s=new Stat(); //creating an instance of Stat class
System.out.println(maximum value:+s.max(10,20));
System.out.println(minimum value:+s.min(10,20));
//execution procedure
Package p1;
Public class A
//body of class A\
In the above example, the package p1 contains one public class by name A.
Suppose we want to add class B to this package. This can be done as follows:
Hiding classes
When we import a package using asterisk(*), all public classes are imported.
If you want to hide these classes from accessing from outside of the package, such
classes should be declared not public.
For example
Package p1;
Public class
//body of class
Class Y
//body of class Y
Here, the class Y which is not declared public is hidden from outside of the
package p1. This class can be seen and used only by other classes in the same
package.
Static import
Static import is another feature introduced with the J2SE 5.0 release. This
feature eliminates the need of qualifying a static member with class name. We can
use the static import statement to import static members from classes and use
them without qualifying class name.
Double area=PI*r*r;
}
Public static void main(String args[])
Mt.circle(3.4);