Home Java Master-java Create Subpackages (i.e. A Package inside another package)



Create Subpackages (i.e. A Package inside another package)
Posted on: March 13, 2008 at 12:00 AM
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:

 

 

Related Tags for Create Subpackages (i.e. A Package inside another package):
javaccomfileideantimportnamespacesiosednamespacenamingpackagesuniqueportnamehierarchymanagedintthisidpackagecreatedefineiecallwithspacestostoreposnamesldwseilspaceitconventionnotlscanputusertpimmanceinnosubrarcalamiasstamntsidsidewanminoscaminosjpackarcaceesallagercmehowintoppacktorwhichweruniqssuspsodefinesdelespatpackhigishellohallmanageeaandarrtwidwantvattssthavsthellohatfinlsoputtingmindonomo


More Tutorials from this section

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
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.