Google+

117. Importing Packages






Suppose, you have two packages in a project and if you want to use a public specified class of package1 in a class under package2, then you have to import the class under package1 into the class under package2 as shown below:

import package1.classname;

Lets find out what happens when we don't import the class under package1 into the class under package2 by implementing this on Eclipse IDE:

1. Create a project say 'Project B1' as shown below:



2. Create two packages named 'package1' and 'package2'  under 'Project B1' project as shown below:



3. Create a public specified class containing any public specified member say method under 'package1' as shown below:



4. Create a class 'ClassP2' under 'package2' which tries to use the 'ClassP1' of 'package1' without importing it as shown below:



5.  Observe that an error is displayed in 'ClassP2' of 'package2' as shown below:



After looking at this error, its very clear that 'ClassP1' of 'package1' is not accessible to 'ClassP2' of  'package2'. We've to import 'ClassP1' of 'package1' in the 'ClassP2' class file to resolve this error.

6. Import 'ClassP1' of 'package1' into 'ClassP2' of 'package2' as shown below:



Observe that the error got resolved and not displayed now.

7. Now access the public member i.e. sample( ) method of 'ClassP1' by calling it as shown below:



8. Save and Run the 'ClassP2' class file
9. Observe that the output is displayed in the console as shown below:



Download this project:

Click here to download this project containing 'ClassP1' and 'ClassP2' class files used in this post (You can download this project and import into Eclipse IDE on your machine)

In the previous example, we've only imported single class from a different package to access it. Now lets try to import more than one class under a package, by importing the package itself instead of importing two classes in separate statements. Instead of writing import statement for each and every class under 'package1' as shown in the below example syntax:

import  package1.ClassP1;  //This statement will only import the 'ClassP1' under 'package1'
import  package1.ClassTwo;  //This statement will only import the 'ClassTwo' under 'package2'

we've to  import the package directly to import all the class files under 'package' at once using the below example syntax:

import  package1.*;   //This statement will import all the class files under the 'package1'

Lets implement importing complete package on Eclipse IDE:

1. In the above 'Project B1', add one more public specified class 'ClassTwo' under 'package1' as shown below and save:



2. In 'ClassP2' under 'package2', import two class files of  'package1' as shown below:



This means, we've written two import statements in order to import two class files under 'package1'

3. Create an object for 'ClassTwo' of 'package1' and access the public specified instance variable 'a' of 'ClassTwo' in 'ClassP2' class as shown below:



4. Save and Run the 'ClassP2' of 'package2'
5. Observe that the output is displayed in the console as shown below:



6. Now replace the two import class files statements with the import package statement as shown below:



7. Save and Run the 'ClassP2' of 'package2'
8. Observe that the same output is displayed in console as it was displayed in the above step 5 as shown below:



At this point, you have to understand that we can directly import the package to import all the classes under the package, instead of importing each and every class separately.

Download this project:

Click here to download the project containing 'ClassP1', 'ClassTwo' and 'ClassP2' class files used in this post (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.


'Interfaces' concept will be explained in the next post.




No comments: