Import Section:
---------------------------------------------------------------------------------------------------------------------The main intention of "import" statement is to make available classes and interfaces of a particular package into the present JAVA file.
Syntax:
---------1. import package_name.*;
-----------------------------
It able to import all classes and interfaces from the specified package.
EX:
import java.io.*;
2. import pacakge_name.member_name;
-----------------------------------
It able to import only the specified member from the specified package.
EX:
import java.io.BufferedReader;
Note: In JAVA file, we are able to provide atmost one package declaration statement but we are abel to provide
more than one import statement.
EX: File Name: abc.java
-------------------------
package p1; ---------> valid
package p2; ---------> invalid ( It should be first statement)
import java.io.*; ---> valid
import java.util.*; -> valid
import java.sql.*; --> valid
----
----
--------------------------------------------------------------------------------------------------------------------------
Q. Is it possible to use classes and interfaces of a particular package without importing that package?
Ans. Yes, it is possible to use classes and interfaces of a particular package in the present java file without importing that package, but we have to use fully qualified names of the classes and interfaces.
Note: Specifying class name and interface name along with package name is called as Fully qualified Names.
EX:
-------
java.io.BufferedReader;
java.util.ArrayList;
Note: In JAVA application, always import statements are suggestible instead a fully qualified names.
EX:
-------
# A JAVA program with import statement.
---------------------------------------------------------
import java.io.*;
----
----
BufferedReader br = new BufferedReader( new InputstreamReader(System.in));
# A JAVA program without import statement.
------------------------------------------------------------
---
---
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(Sytem.in));
Note: Here java.io.BufferReader is fully qualified name.
No comments:
Post a Comment