You are on page 1of 2

package

--------
A package is a collection of classes,
interfaces and nested packages.
Why Packages?
--------------
A package is used to make
* Java Projects/Applications
* Java Libraries
To create a package:
-------------------
Create a directory of the same name
as of the package.
To put classes and interfaces in the package:
---------------------------------------
* Define the classes and interfaces with package statement as their first statem
ent.
* Place the classes and interfaces in the package directory.
How to use a package?
--------------------
If the package is representing a project/application
then generally it is executable/deployable.
If the package is representing a library
then its "public" classes and interfaces
are "imported" and used by other classes
and interfaces.
What is import?
---------------
"import" is a java statement that allows
a class to access public classes/interfaces
of an available package.
How to use import?
-----------------
import is used as follows:
import pckgName.className;
import pckgName.*;
How is package statement resolved?
--------------------------------
To resolve the package statement
system uses :-
environment variable "classpath"
or
"-classpath" compilation and execution option.
-------------------------------------
To set classpath
Right click on my computer
Click on properties
Select Advanced
Select Environment Variables
Under System
Edit/Create
variable name : CLASSPATH
variable value : <existing entries>;locationWherePackageIsPlaced
OK to all windows
---------------------------------------------
To set classpath as compilation/execution option
c:\java>javac -classpath "%classpath%";e: AboutProgramming.java
c:\java>java -classpath "%classpath%";e: AboutProgramming
---------------------------------------------
Rule for public class/interface
It must be saved in a file that has
same name as of the class/interface.

You might also like