Skip to main content

Import Statement || Java OOps

Import Statements

1. Length of the code is Reduced.
2. import Statement is used once.
3.Advantage is used for Shortnames.

Simple Program1:
class Test
{
public static void main(String []args)
{
ArrayList l = new ArrayList();// short names cannot be acceptable with import Statement
}
}
Output: Cannot Find Symbol(error)
                        Symbol: Class ArrayList
                        Class: Test

Solution:
Simple Program2:
class Test
{
public static void main(String []args)
{
java.util.ArrayList l = new java.util.ArrayList(); //Fully qualified name
java.util.ArrayList l = new java.util.ArrayList();
}
}
Output:
The program is executed
*Note: java.util.ArrayList   is  used for everytime ( if we use inside the body).

Simple Program3:

Import java.util.ArrayList; // there is no need to Fully qualified name.
class Test
{
public static void main(String []args)
{
ArrayList l = new ArrayList
}
}
Output: Import statement is executed

There are Two types of import Statements
1. Explicit import Statement
2. Implicit import Statement

1.   Explicit import Statement

·       Readablity of program is more.
·       Statement is used for evey class. (recommended mostly)

Import com.hdfc.ArrayList;
.
.
ArrayList l = new ArrayList


2.   implicit import Statement

·       Readablity of program is not good
·       Statement is used for evey class.

Import com.hdfc.*;

ArrayList l = new ArrayList // package not find and matched  class






Comments