Java Import Classes

Java Import Classes: A package is the style of Java to group the classes and interfaces together, or to say, a package is a collection of related predefined classes and interfaces. Packages are sets of classes and interfaces built into Java Development Kit (JDK). A package places restrictions (to the classes of other packages) to access its enclosed classes. Access specifiers work on package boundaries (between the classes of different packages).

Java Import Classes: Definition and Importance

A package is a group of related classes and interfaces. The JDK software is organized through packages. It is equivalent to a header file of C-lang and module of Modula language (Modula is a descendent of Pascal). Packages can be compressed into JAR files (refer xxxx) so that they can be downloaded or sent across the network fast.

Java Import Classes: Advantages of Packages

1. With a simple import statement, all the classes and interfaces can be imported.
2. Java includes a provision to import only one class from a package.
3. It avoids namespace problems (name conflicts). Two classes with the same name cannot be put in the same package but can be put in two different packages because a package creates its own namespace (folder).
4. Access for the classes can be controlled.
5. Classes and interfaces of same functionality can be grouped together.
6. Because functionally all the classes are related, later their identification and determining the location become easier.
7. Java packages are used to group and organize the classes.

Java Import Classes: Importing All/Single Class

Java permits to import all the classes or only one class from a package. C-lang does not have this facility of including only one function from a header file.

		import java.util.*;            	   // imports all the classes and interfaces
		import java.awt.event.*;            // imports all the classes and interfaces

		import java.util.Stack;                               // imports only Stack class
		import java.awt.event.ActionEvent;         // imports only ActionEvent class
							      // observe, no asterisk, *

Importing Classes – Different Approaches

There are three ways to access a class or an interface that exits in a different package.

1. Using fully qualified name
2. Importing only one class
3. Importing all the classes

1. Using Fully Qualified Name

Fully qualified name includes writing the names of the packages along with the class name as follows.


Objects can be created as follows.

java.util.Stack st = new java.util.Stack();

This way of using may not look nice when we use the same class a number of times in the code as readability becomes boredom. This will be a nice approach and justified when the class is used only once.

2. Importing Only One Class

Java permits to import only one class also from a package as follows.

import java.awt.event.ActionListener;
import java.util.Stack;

After importing, the objects of the classes can be created straightaway.

Stack st = new Stack();

This type of approach is the best and beneficial when only one class is required from the same package. Importing one class (when other classes are not required) saves a lot of RAM space usage on the client’s machine.

3. Importing All The Classes

One of the advantages of packages is to include all the classes and interfaces of a package with a single import statement.

import java.awt.event.*;
import java.util.*;

The asterisk (*) mark indicates all the classes and interfaces of the package. After importing, the objects of the cl
asses can be created straightaway.

Stack st = new Stack();

This type of approach is beneficial when many classes and interfaces are required from a package.

Java Import Classes: Packages are discussed as here under.

1. Predefined Packages – Java API
2. Creating Custom-Defined Packages

Leave a Comment

Your email address will not be published.