Home
| | Sitemap
||Page number :21
Using Library Classes
Package
In Java, you can group a number of classes into a single unit known as packages which also act as a container for a group of classes.
Example---
There are several inbuilt java packages. They contain groups of classes that help us in writing our program. java.io Java Input-Output package java.lang Java language package
Defining a Package
A programmer, if desires can create his own package. This can be really helpful in certain conditions. Syntax— package name;
Remember: A class that defines itself inside a package “pack” should be placed inside a folder (directory) “pack”. In this way a class hierarchy can be built. A hierarchy of packages and even folders can thus be created.
Study this example for a better knowledge of packages.
package first;
class school { String name; int marks;
public school() { name="Priyanka"; marks=94; }
public void show() { System.out.println("Printing Student's name and marks"); System.out.println(“Name is "+name); System.out.println(name +"'s"+" marks are "+marks); } }
class sch { public static void main(String args[]) { school obj=new school(); obj.show(); } }
In this example we have constructed a package named first that contains two classes named school and sch. Save this program as “sch.java” under a folder “first”. Use the cd command in DOS to enter the directory “first”. Compile the program in the usual way.
javac sch.java; For executing the program, move above the “first” directory i.e. in the folder containing the “first” directory using the command “cd..”. Then execute the program in this way----
java first.sch As the class sch is in the package first, we can not compile the program using the normal way.
java sch // Wrong way
Classpath Woes
It becomes difficult for Java to search a package. As packages are as good as folders, it will be impossible for java to recognize the location of our package. In the example mentioned above we successfully created a package “first” and executed the code. This is because at the time of execution we were in a directory (Java Prgs) that was just above “first” in the folder’s hierarchy. By default, java searches the specified package in the subdirectory of the folder in which we are working.
If you are not in the directory that is just above the “package” directory (Have a look at the screenshot that “Java Prgs” is the directory in this case), you have to specify a classpath to java runtime system. A classpath simply informs the runtime system about the location of our package.
Importing Package
Look at the program in the previous chapter that demonstrates the use of “throws” keyword. We have imported the “io” package in the program.
The Line goes like this… import java.io.*; This might give you some understanding of using a particular package. The keyword “import” helps us in this. I have created three packages named highest, higher and high. Suppose that “higher” lies inside the package “highest”. Higher too contains a package “high”. The high package contains three classes. Let their names be first, second and third. If you want to import the class “second.java”, you will write
import highest.higher.high.second; There is an alternative to this method—
import highest.higher.high.*; If you use the “import” statement in this way, all the classes will be imported including “second.java” which we desire to import. Can you guess the reason behind it? It is due to the “*”. Here “*” works as a wild card character that represents all the classes.
General Syntax— import package1.package2.package‘n’.classname;
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29