User Define Package

Author: Ravi Poswal

In java, the user can create their package container using the “package” keyword. Java files are kept in file system folders. In the package, a user should follow some protocol.

  • The package name should be all lower case if possible.

  • Organizations can choose a specific name for their package.

  • Start the package with the term "package."

  • All related classes should belong to the same package.

Syntax :-    package mypack;

                     public class A

                                                 {

                                                            ----------   }

Code Example

  package p1; 
                          class A
                                        {
                                           public static void main(String arg[])
                                                { 
                                                System.out.println("package p1 with class A"); 
                                                p2.B x= new p2.b();
                                                System.out.println("Invoking display() B of p2”);
                                                x.display();
                                                p3.B y = new p3.B();
                                                System.out.println("Invoking display() B of  p3");
                                                y.display();
                                                 }
                                         }
            package p2; 
                         public class B
                                     {
                                              public void display()
                                              {
                                              System.out.println("display() of B of p2 invoked");
                               }    
}
            package p3; 
                         class B
                                   {
                                      public void display()
                                           {  
                                           System.out.println("Display() o f B of p3 invoked");
                                             }  
}