Packages
========
Packages act as "containers" for classes.
Two types of packages:
Java API
packages
User defined
packages.
Package name Constents
java.lang Language support
classes. These are classes that java
compiler itself uses and therefore they are automatically imported. They include classes for primitive
types,strings,math functions,threads and exceptions.
java.util Language utility
classes such as vectors,hash tables,random numbers,date,etc.
java.io Input/Output support
classes. They provide facilities for the
input and output of data.
java.awt Set of classes for
implementing graphical user interface.
They include classes for windows,buttons,lists,menus and so on.
java.net Classes for
networking. They include classes for
communicating with local computers as well as with internet servers.
java.applet Classes
for creating and implementing applets.
Using System Packages:
========================
java.awt.*; -
will bring all classes of java.awt package.
or
java.awt.
Color
Graphics
Font
.
.
Image
That
the package named java contains the package awt
package
containing classes
classes
containing methods
syntax:
import
packagename.classname;
or
import
packagename.*;
import statement must appear at the top of the file, before
any class declarations,
import is a keyword.
Naming Conventions
===================
packages begin with lowercase letters.
class name begin with an uppercase letter.
double
y=java.lang.Math.sqrt(x);
lang - package name
Math
- class name
sqrt(x) - method name
Creating Packages
---------------------
We must first declare the name of the package using the
package keyword followed by a package name. This must be first statement in a
java source file(Except for comments and white spaces).
ex:
package
firstPackage;
public class
FirstClass
{
...........
...........
...........
}
saved as a
file called FirstClass.java, and located in a directory named
firstPackage. When the source file is
compiled, Java will create a .class file
and store it in the same directory.
The .class
files must be located in a directory that has the same name as the package, and
this directory should be a subdirectory of the directory where classes that
will import the package are located.
Own package involves the following steps:
1. Declare the
package at the beginning of a file using the form
package
packagename;
2. Define the
class that is to be put in the package and declare it public.
3. Create a
subdirectory under the directory where the main source files are stored.
4. Store the
listing as the classname.java file in the subdirectory created.
5. Compile the
file. This creates .class file in the
subdirectory.
Accessing a Package
====================
syntax:
import
package1 [.package2] [.package3].classname.*;
Here
package1 is the name of the top level package, package2 is the name of the
package that is inside the package1, and so on.
The
statement must end with semicolon(;).
Using a package
==================
package package1;
public class ClassA
{
public void
displayA()
{
System.out.println("Class
A");
}
}
-------------
import package1.*;
class PackageTest1
{
public static
void main(String args[])
{
ClassA
a=new ClassA();
a.displayA();
}
}
No comments:
Post a Comment