Home Java Master-java Create Subpackages (i.e. A Package inside another package)
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Create Subpackages (i.e. A Package inside another package)

Advertisement
We can also put a package inside an another package. The packages that comes lower in the naming hierarchy are called "subpackage" of the corresponding package higher in the hierarchy i.e. the package that we are putting into another package is called "su

Create Subpackages (i.e. A Package inside another package)

     

We can also put a package inside an another package. The packages that comes lower in the naming hierarchy are called "subpackage" of the corresponding package higher in the hierarchy i.e. the package that we are putting into another package is called "subpackage". The naming convention defines how to create a unique package name, so that packages that are widely used with unique namespaces. This allows packages to be easily managed. Suppose we have a file called "HelloWorld.java". and  want to store it in a subpackage "subpackage", which stays inside package "importpackage". The "HelloWorld" class should look something like this:

 

package importpackage.subpackage;

public class HelloWorld {
  public void show(){
  System.out.println("This is the function of the class HelloWorld!!");
  }
}

Now import the package "subpackage" in the class file "CallPackage" shown as:

 

import importpackage.subpackage.*;

class CallPackage{
  public static void main(String[] args){
  HelloWorld h2=new HelloWorld();
  h2.show();
  }
}

Out of the program:

C:\nisha>javac C:\nisha\importpackage\subpackage\HelloWorld.java

C:\nisha\importpackage\subpackage>cd..

C:\nisha\importpackage>cd..

C:\nisha>javac CallPackage.java

C:\nisha>java CallPackage
This is the function of the class HelloWorld!!

Make sure to the directory structure for this program shown as:

 

 

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 13, 2008

Ask Questions?    Discuss: Create Subpackages (i.e. A Package inside another package)   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
prince
April 18, 2011
thank you

thank you for this information.
prince
April 18, 2011
thank you

thank you for this information.
DMCA.com