wellnwill Marathahalli Bangalore

How to access the classes or interfaces present in different package


There are two ways to access the classes or interfaces present in different package.

  • By using fully qualified class name.
  • By using import statement.

If we want to access any class or interface from another package by using fully qualified class name then we have to access the class with the package name.

Ex- com.techbrothers.p1.Test

package com.techbrothers.p1;
public class Test
{  
   public void display()
	{
		System.out.println("dipaly() in p1");
	}
    public static void main(String args[]){	
    System.out.println("Hello package p1");
    }
} 	
package com.techbrothers.p2;
class Test181
{  
	public static void main(String args[]){	
	com.techbrothers.p1.Test t1=new com.techbrothers.p1.Test();
	t1.display();
              System.out.println("Hello package p2");
    }
} 	




  • By using fully qualified name everytime increases the length of the code and reduces program readability.
  • We generally use this approach when two packages have same class name. Ex- date class present in both java.util and java.sql package.
import java.util.*;
import java.awt.*;
class Test184
{  
	public static void main(String args[]){	
	List l1=new List();
	System.out.println(l1.getClass().getName());
    }
} 
//O/P-CTE-reference to List is ambiguous.

import java.util.*;
import java.awt.*;
class Test185
{  
    public static void main(String args[]){	
	// using fully qualified name
    java.awt.List l1=new java.awt.List(); 
    System.out.println(l1.getClass().getName());
    }
}
//O/P- java.awt.List

By using import statement

In java, the predefined classes are maintained in the form of packages. To make available predefined classes or user defined classes from other package to our program we use import statement.

Types of import

There are 2 types of import statements.

Explicit class import

import .;
Ex- import java.lang.System;

In above example, we can access only System class from lang package.

Implicit class import

import .*;
Ex-import java.lang.*;

In above example, we can access all the classes present inside the lang package.

package com.techbrothers.p1;
public class Test
{  
   public void display()
	{
	System.out.println("dipaly() in p1");
	}

   public static void main(String args[]){	
    System.out.println("Hello package p1");
    }
} 	
package com.techbrothers.p2;
import com.techbrothers.p1.Test;
class Test182
{  
	public static void main(String args[]){	
	Test t1=new Test();
	t1.display();
              System.out.println("Hello package p2");
    }
} 	








In the above programs, we are accessing Test class (which is present in p1 package) from Test182 class (which is present in p2 package) through import statement.

While resolving class name compiler will always give the precedence in the following order

  • Explicit class import
  • Classes present in current working directory
  • Implicit class import
import java.util.Date;        // explicit import
import java.awt.*;             // implicit import
class Test183
{  
	public static void main(String args[]){	
	Date d1=new Date();
	System.out.println(d1.getClass().getName());
    }
}
// O/P- java.util.Date	

Note: If we are importing a package then all classes and interfaces of that package will be imported but the classes and interfaces of sub packages will not be imported.

If we want to use the classes and interfaces of sub packages then we have to write import statement until sub package level.

Static import

In general we can access static members of the class by using class name.

With the help of static import, we can access static members i.e. static variables and static methods of the class directly without using class name.

Types of static import

There are two types of static import statements.

Explicit static import

import ..;
Ex- import static java.lang.Byte.MAX_VALUE;

In above example, we can access public static variable MAX_VALUE of Byte class.

Implicit class import

import .*;
Ex-import static java.lang.Byte.*;

In above example, we can access all the public static members of Byte class .

package com.techbrothers.p2;
class Test0187{  
  public static void main(String args[])
  {	
   System.out.println(Byte.MAX_VALUE);// accessing public static variable MAX_VALUE through class reference
   System.out.println(Byte.MIN_VALUE);  // accessing public static variable MIN_VALUE through  class reference
  }
}
//O/P- 127
       128

package com.techbrothers.p1;
public class Test1
{  
   public static void display()
	{
	   System.out.println("dipaly() in p1");
	}
  public static void main(String args[]){	
    System.out.println("Hello package p1");
    }
} 	
package com.techbrothers.p2;
import static com.techbrothers.p1.Test1.display;
class Test186
{  
	public static void main(String args[]){	
	display(); // calling public static display() without using class reference.
              System.out.println("Hello package p2");
    }
} 	



Note

Normal import never talks upto member level.

Static import always talks upto class or member level.

While resolving class memebers compiler will always give the precedence in the following order:

  • Current class static member
  • Explicit class import
  • Implicit class import
package com.techbrothers.p2;
import static java.lang.Byte.MIN_VALUE;    //   explicit static import 
import static java.lang.Double.*;                   //   implicit static import   
class Test188
{  
    static byte MIN_VALUE=100;                     // Current class static member
	public static void main(String args[]){	
	System.out.println(MIN_VALUE);
    }
} 
//O/P-100	

Explanation of System.out.println() statement:

public final class System
{
 public static final PrintStream out=….;

}  
class PrintStream
{
  public void println(String s)
   { 
      /* it prints the specified string value and terminates the line*/
   }
}

println():

It is a non-static method of PrintStream class.

out:

out is a static variable present in System class of PrintStream type.

System:

it is a class which is defined in java.lang package.

Footer with Map

Top