Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Master-java How to import a package

Related Tutorials


 
 

Share on Google+Share on Google+

How to import a package

Advertisement
There are two ways so that you can use the public classes stored in package.

How to import a package

     

There are two ways so that you can use the public classes stored in package.
  • Declaring the fully-qualified class name. For example,
mypackage.HelloWorld helloworld = new mypackage.HelloWorld();
  • Using an "import" keyword: For example,
       import world.*;    // we can call any public classes inside the mypackage package 

Lets see an example importing the created package into the another program file.

 

package importpackage;

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

In this program the package "importpackage" is created under the "c:\nisha" directory that will be imported into another program file to call the function of the class "HelloWorld".
Now make a program file named "CallPackage" under the "c:\nisha" directory  importing the package "importpackage".

 

import importpackage.*;

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

Out of the program:

C:\nisha\importpackage>javac *.java

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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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 10, 2008

Related Tutorials

Discuss: How to import a package   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
gautam muni singh
July 5, 2011
why error show??

package Mypack; class Simple{ public void msg1(){System.out.println("gauti");} } package Yourpack; import Mypack.*; class P1{ public static void main(String args[]){ Simple a=new Simple(); a.msg1(); } }
chinmay
October 28, 2012
replay

sorry!!!but this is not working.....
DMCA.com