/* related posts with thumb nails */

Steps to Create a Package:

Package is a group of classes created in a unique directory. Upon importing a package in a program, we will be able to access the classes defined in that package.  The following procedure shows step-by-step approach to create a package.

1.      Create a sub-directory in your working directory with the package name you would like to have. Use the following syntax:
c:\jdk1.5\bin>md p1
2.      Move to that directory using the following syntax. Note that the path changes.
c:\jdk1.5\bin>cd p1
3.      Open a new file using an editor in the same directory.
c:\jdk1.5\bin\p1>edit Test.java
4.      Make sure that the class name is public. Here the class name should be Test.
5.      Also, place the package statement in the program, now it looks like this
package p1;
public class Test
    public void sayHello( )
      System.out.println(“Hello from Package”);
6.      Compile the file in the same directory using the syntax: (the program and class file can be created elsewhere, but it is a must to place class file in this directory)
c:\jdk1.5\bin\p1>javac Test.java
7.      Repeat from step no.3 if you wish to add more classes to the same package.
8.      Move back to the working directory using the following syntax:
9.      Now, you may create a program to test the package. Make sure to import the package.
import p1.*;
class TestPack
  public static void main(String as[])
    Test t1 = new Test( );
    t1.sayHello(); // Prints: Hello from Package
Related Topics:


Post a Comment