Page 81 - CA_Blue( J )_Class10
P. 81
5.1.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 too much code. These packages contain several built-in Java classes
in them. Some of the built-in packages are:
java
util lang io awt swing sql
Built-in Packages in Java
All these built-in packages follow a hierarchy. You can see in the preceding diagram that all the packages are inherited
from the Java package. Furthermore, they also have their sub-packages and classes in them.
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 the following
way:
java.util.Calendar obj = java.util.Calendar.getInstance();
Calendar is an abstract class of the 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>.<sub-package-name>.<class-name>;
Where, import is the keyword, <package-name> is the name of the package which is always java, <sub-package-
name> can be the name of any sub-package under the java 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
The preceding statement imports only the Calendar class and its variables and methods only. If you want to import all
the classes from 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
Program 1 To import a package and use its class.
1 import java.util.*;
2 public class Cal {
3 public static void main()
4 {
5 Calendar cr = Calendar.getInstance();
6 System.out.println("Today is: " + cr.getTime());
7 }
8 }
79
Input in Java 79

