Importing Packages

When we declare Java files to different packages, then we can import data members and member functions to other Java classes for using them. To do this first you need to create two packages or directories in a parent directory. Suppose your current directory is homeDir. create two packages in this directory as :-
1. Package 1
2. Package 2
now create PackageExample.java in Package 1 whose code is :

package package1;

import package2.ImportExample; // specific class name after package name 
//to import only one class, put * for all classes import

public class PackageExample {
	public static void main(String[] args) {
		ImportExample im = new ImportExample(); // object initialization for
												// imported class
		System.out.println(im.importFunction()); // call imported class method
	}
}

create ImportExample.java in package2. Code as :

package package2;

public class ImportExample {
	public String importFunction() {
		return "import success";
	}
}

run the PackageExample.java to see the action