Back Button
Back to Chapter
Introduction
No items found.
Collapse_Icon
Table of Contents

To import the java package into a class, we need to use the java import keyword which is used to access the package and its classes into the java program.

Use import to access built-in and user-defined packages into your java source file to refer to a class in another package by directly using its name.

syntax:

import package.name.ClassName;   // To import a certain class only
import package.name.*   // To import the whole package

Example:

import java.util.Date; // imports only Date class
import java.io.*;      // imports everything inside java.io package

Example for creating and importing java packages:

//save by Greet.java  
package greeting;  // creating package
public class Greet{  
   public void msg() {
       System.out.println("Hello");
   }  
}

//save by Main.java  
package Java;  
import greeting.*;    //importing package
class Main {  
   public static void main(String args[]) {  
       Greet g = new Greet();  
       g.msg();  
   }  
}

/*OUTPUT:
Hello  */