How to create java package programs

 Hai now i show how to write java package programs and explain with examples


Step 1.
first create a java package like
note:  $ we must create sub directory to store the package
         $ package name and sub directory name are same
$ create a directory like
          c:\program files\java\jdk1.5.0\bin>md student
$ enter the sub directory like
          c:\program files\java\jdk1.5.0\bin>cd student
          c:\program files\java\jdk1.5.0\bin\student>
Step 2.
now we can type the package program
syntax : package packagename;
example :
         c:\program files\java\jdk1.5.0\bin\student>edit
package student;
public class p1
 {
    public void display()
       {
          System.out.println(" this is package");
        }
}

$ save the package as p1.java
$ and exit the program
Step 3.
compile the package program like
          c:\program files\java\jdk1.5.0\bin\student>javac p1.java
with no error go for next step
Step 4.
type the main program
here we just come out the sub directory and create the program like
         c:\program files\java\jdk1.5.0\bin\student>cd..
         c:\program files\java\jdk1.5.0\bin>
go for edit
         c:\program files\java\jdk1.5.0\bin>edit
              
                import student.p1;
                class p2
                  {
                     public static void main(String args[])
                        {
                            p1 ob = new p1();
                            ob.display();
                         }
                   }

$ save and exit the program like p2.java
$ compile and run the program like
          c:\program files\java\jdk1.5.0\bin>javac p2.java
$ without error
         c:\program files\java\jdk1.5.0\bin>java p2
          this is package
         c:\program files\java\jdk1.5.0\bin>

Post a Comment

0 Comments