USING PACKAGES – THE IMPORT STATEMENT

Example

Remember the Illustration of the four families as shown in the picture above? Click here to refresh you memory. I explained that family “A” member can call members of families B, C and D and vice versa. Today, I will be explaining how this could be achieved, I promised three things in my last post on package: 1.)How to make a class callable outside its package, 2.)The import statement and 3.) The different styles of import. Hence, to fulfill my promise I will be providing answers to these key points today.

  1. How to make a class callable outside its package: Since only public classes are accessible outside their package, the classes to be accessed must be modified with the “public” keyword. Please refer to my previous post on Java Modifier for more explanation.
  1. The import statement: In Java, calling a member of another package is done using the Import statement. The import statement is given the address of the package and public classes within the package. The import statement is the second statement on a java file after the package declaration.

Example:  import java.util.*;

  1. Different styles of importing a package:
  • Import on demand – Import on demand calls the entire package with only one import statement hence providing access to all public classes within the package. This style of import is identified with the asterisk (*) wildcard character.

Example:

    import java.util.*;         * calls all public classes on the package “java.util”

Implication: One import statement is sufficient to access all the public classes within the packages.

 ondemand

 With “import A.*;” Surveyor and Bricklayer classes can be accessed.

  • Single-type Import – Single type import calls a specific public class on a package. E.g.

import java.util.List;        .List calls class List on package java.util

Implication: To access any other class on the package apart from the specified class will require another import statement for the new class.

singleimport

With “import A.Surveyor;” Surveyor class can be accessed but Bricklayer class cannot be accessed hence the need to add “import A.Bricklayer;”

  • Qualified Name – This style of import doesn’t require the import statement but rather, a reference is made to the whole package when the class is needed.

java.util.List mathCalculator= null; full package name of the class is referenced when the class is needed.

 Implication: Each time access to class is required, the qualified name will be prefixed to the class name.

qualifiedname

With “A.Surveyor;” Surveyor class can be accessed and Bricklayer class with “A.Bricklayer;”

  • Static Import – Import static keyword is used to access static members of a class. The members of a class (methods and fields) that can be accessed must be declared with the static keyword.

import static java.lang.Math.*;         insert static between import and the class full name and postfix the statement with “.*”.

Implication: Allows access to any static member of an imported class directly without using the class name.

 staticimport

  • Automatic Import – This style of import is implicit and does not require any import statement as they are imported by default.

java.lang package falls in this category and does not require import statement. This is because the package contains classes that are fundamental to the design of the Java programming               language and nothing can be done in Java without them. Also, sibling classes (classes within the same package) need no import statement since they belong to the same package.

String test; calls class “String” without any import statement.

Implication: Allows access to class without any import statement.

Example of classes in this package are: Double, String, Exception, System, etc.

 autoimport

 I hope this tutorial made some sense to you, feel free to contact me should you have any contribution or concerns. Until next time, happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s