X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Third party packages :

Third party packages are those which are developed by third party software vendors and released to the real industry as a part of third party products (Databases etc.). Example : oracle.jdbc.driver.OracleDriver


How to use packages, classes and interfaces in another Java program :

In order to refer package's classes and interfaces in Java we have two approaches, they are using import statement and using fully qualified name approach.


Using import statement :

import is a keyword which is used to import either single class or interface or set of classes and interfaces all at once.

Syntax - 1 : import pack1 [.pack2 [.………[.packn]]].*; For example: import p1.*; // Statement 1 import p1.p2.*; // Statement 2 import p1.p2.p3.*; // Statement 3 When statement 1 is executing we can import or we can access all the classes and interfaces of package p1 only but not its sub packages p2 and p3 classes and interfaces. When statement 2 is executing we can import as the classes and interfaces of package p2 only but not p1 and p3 classes and interfaces. When statement 3 is executing we can import as the classes and interfaces of package p3 only but not p1 and p2 classes and interfaces.

Syntax - 2 : import pack1 [.pack2 [.…………[.packn]]].class_name/interface_name; For example: import p1.c1; ---4 When statement 4 is executing can import c1 class of package p1 only but not other classes and interfaces of p1 package, p2 package and p3 package.

import approach :

import p1.Example;
class Pack1
{
  public static void main (String[ ] args)
 {
   Example t1=new Example();
   t1.show();
 }
} 

When we compile the above program we get the following error “package p1 does not
exist”. To avoid the above error we must set the classpath as., SET CLASSPATH =
%CLASSPATH%;.;

This is the alternate technique for import statement:

p1.c2 o2=new p1.c2 ();
p1.p2.p3.c4 o4 = new p1.p2.p3.c4();
p1.p2.i3 o3 = new p1.p2.p3.c4();

Fully Qualified Name approach :

This approach is also called as canonical form. It is one of the alternative approach for referring the classes and interface instead of import statement. By this approach we can refer either a class or interface but not possible to refer all the classes and interfaces of the package.

Fully qualified approach :

class Pack1
{
 public static void main (String[ ] args)
 {
  p1.Example t1=new p1.Example();
  t1.show();
 }
}


Note:

  • Whenever we develop user defined packages, to use the classes and interfaces of user defined packages in some other program, we must set classpath before their usage.

  • In order to set the classpath for predefined packages we must use the following statement : D:\core\set classpath = C:\Program Files\Java\jdk1.5.0\lib\rt.jar;.; [rt.jar contains all the .class files for the predefined classes which are supplied as a port of predefined packages by the Sun Microsystem].

X

Sun Microsystem :

Sun Microsystems, Inc. was a company that sold computers, computer components, computer software, and information technology services and that created the Java programming language, Solaris Unix and the Network File System.

On January 27, 2010, Sun was acquired by Oracle Corporation for US $7.4 billion, based on an agreement signed on April 20, 2009. The following month, Sun Microsystems, Inc. was merged with Oracle USA, Inc. to become Oracle America, Inc.

  • When two classes or interfaces belong to the same package and if we want to refer those classes or interfaces then we don't have to refer it by its package name.

  • Whenever we are using import statement it is not required to use fully qualified name. Hence, it improves readability and reduces the length of the code.

Types of import statements :

There are two types of import statements :-

  • Explicit class import




  • Implicit class import




Explicit class import :-

This type of import is highly recommended to use because it improves readability of the code. Example : import java.util.ArrayList;


Implicit class import :-

This type of import is not recommended to use because it reduces readability of the code. Example : import java.util.*;

Example :

import java.util.*; import java.sql.*; class Example { public static void main(String[ ] args) { Date d = new Date(); System.out.println("Algo"); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

Compile time error : reference to Date is ambiguous, both class java.sql.Date in java.sql and class java.util.Date in java.util match

Example :

import java.util.Date; import java.sql.*; class Example { public static void main(String[ ] args) { Date d = new Date(); System.out.println("Algo"); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

Algo

Whenever we are importing a package then all the classes and interfaces present in that package are available but not the classes and interfaces of subpackage.

The following two packages are not required to import because all classes and interfaces present in these two packages are available by default to every Java program :-

  • java.lang package




  • default package (current working directory)




static import :

This concept is introduced in version 1.5 of Java. According to Sun Microsystem static import improves readability of the code. Usually we can access static members by using class names but whenever we are using static import, it is not required to use class name and we can access static members directly.

Example :

import static java.lang.Math.*; class Example { public static void main(String[ ] args) { System.out.println(sqrt(4)); System.out.println(random()); System.out.println(max(10,20)); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

2.0 0.9979451771040758 20

Example :

import static java.lang.Integer.*; import static java.lang.Byte.*; class Example { public static void main(String[ ] args) { System.out.println(MAX_VALUE); } } //save as : Example.java //compile as : javac Example.java //run as : java Example

Output :

Compile time error : reference to MAX_VALUE is ambiguous, both variable MAX_VALUE in Integer and variable MAX_VALUE in Byte match
Note :

Two classes containing a variable or method with same name is very common. Hence ambiguity is common in static import.


While resolving static members compiler will always give the precedence in the following order :-

  • Current class static members




  • Explicit static import




  • Implicit static import




We can use normal import to import classes and interfaces of a package. Whenever we are using general import it is not required to use fully qualified name and we can use short names directly.

We can use static import to import static variables and methods of a class. Whenever we are using static import then it is not required to use class name to access static member i.e. we can access it directly.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.