Free WebSchools.com

Google




Blog ForumsLive chat WebhostingLink TO UsLink TO UsSEO Tools


HOME

J2ee tutorials

Java introduction
Java basic
Java installation
Java Packages
Learn Applets
Java Threads
Java Gui component
pixels


Java Event Handaling
Java Streams and Files
pixels
Java Swings
Java JDBC
pixels
Java Network Programming
Java RMI
pixels
Java Servlets
Java javabeans
pixels
EJB
Struts
pixels


what is package???????



Imagine your hard drive contains all the files in a single folder. In such case it may become difficult to manage. So at that time it is better to create sub directories. so java provides us packages. A package can be thought of as sub-directories. In other words packages are group of related classes and interfaces. Packages provide a convenient mechanism for managing a large group of classes and interface while avoiding potential naming conflicts.



Creating packages



To create the package you have to follow these steps

step1 create a file containing the package defination.Package is defined by including package command as the first statement in the java source file.

step2 create a folder having the same name as the package name. Save the source file inside that folder.

step3 compile the source file

Let’s take the example
We have to create package color. This package contains class dispalycolours. This is shown below



package color;
public class displaycolour
{
protected String a="white";
protected String b="red";
protected String c="green";
public void disp()
{
System.out.println("this package shows colorcombinations");
System.out.println("first combination= "+a+b);
System.out.println("second combination=" c+b);
}
}


But note while creating packages. The classes should be in same directory as the rest of the package files. The package statement should be the first statement in the file. Package name begins with lowercase letters.Packages names begin with lowercase letters, which makes them different from class names.

Importing packages

The import statement enables you to import classes form other packages into a compilation unit. You can import individual classes or entire packages of classes at the same time if you want.
the syntax is

import idetifier1.identifier2;
identifier1 is the name of the package
identifier2 is the name of the classes
considering the name of color package we can import the classes by following any of the two ways

import color.displaycolour
import displaycolour.*;
Now we will see how to use these package in our program

import color.*;
class testcolor
{ public static void main(String args[])
{
displaycolour c1=new displaycolour();
c1.disp();
}
}


Inform Friend About This Site

Your Name:

Friend's Name:

Friend's Email: