Page 123 - CA_Blue( J )_Class9
P. 123
6.2 PACKAGES IN JAVA
A package is like a folder in a computer where we can store similar types of files and sub-folders. In the same way,
a package in Java can store similar types of Java classes and packages. A package inside another package is known
as a sub-package.
Each class within a package can use the public properties Definition
and behaviours of other classes. This saves time and effort, A package is a collection of Java classes and
making the program efficient. A package is also helpful in other packages having similar functionality.
resolving any issue related to naming conflicts if a class has
the same name as any predefined Java class.
For example, if you define a class named Date, there might be a naming conflict if your code imports java.util.Date.
However, if you place your Date class in a package, you can avoid this conflict by using the fully qualified name, and
the Java compiler will recognise it as distinct from the Date class in the Java class library.
6.2.1 Built-In Packages in Java
The packages that are already defined in the Java class library are called built-in packages. Java has a set of built-in
packages that are available to use without writing long codes. These packages contain several built-in Java classes
in them. Some of the built-in packages are:
JAVA
util lang io swing sql
Using the Built-in Packages
Suppose you want to use the Calendar class of the java.util package in your program, then you can use it the
following way:
java.util.Calendar obj = java.util.Calendar.getInstance();
Calendar is an abstract class of java.util package, hence it cannot be directly instantiated. You need to use the
getInstance() method to create an instance of the Calendar class. The drawback of using the built-in class in this
manner is that you need to type the complete hierarchy of the class each time when you want to use the class. So,
this process is very time consuming and it makes the program difficult to understand. To overcome this problem,
Java provides the import statement to use a class of a package in your program. The syntax of the import statement
is:
import <package-name>.<subpackage-name>.<class-name>;
Where, import is the keyword, <package-name> is the name of the package <subpackage-name> can be the name
of any sub-package under the package and <class-name> can be the name of a particular class which you want
to import in your program. Similar to any other Java statement, every importing statement should be terminated
by placing a semicolon at the end of each statement. For example, if you want to import the Calendar class of the
java.util package, then:
import java.util.Calendar; //Importing the Calendar class of java.util package
Preceding statement imports only the Calendar class and its variables and methods. If you want to import all the
classes form a package, you just need to use the * (asterisk) symbol with your package name. For example,
import java.util.*; //Importing all the classes of java.util package
Input in Java 121

