Previous | Home | Next |
Import is a keyword . Import keyword is used built in or user defined packages into java source code file. When use package in our source file then access the package class with import keyword declaration. The compiler can access any class in the java.lang package without needing an import statement.
import mypackage.*; import mypackage.extrapackage.*; import java.applet.*;
Indicate that classes can be found in the mypackage and mypackage.extrapackage packages and also that the Applet class can be found in the java.applet package.
The first import statement gives the full package path name for the Applet class in the packages included with the standard JVM, then when the compiler sees just Applet in the class definition title, it will know where to find it. In the next two import statements, we see the "*" wildcard character used for the classes in the mypackage and mypackage. extrapackage packages. This indicates that these packages hold multiple classes and the compiler should look here if it cannot find a class in the core packages.
Example :
import java.util.Scanner; public class ScannerTest { public static void main(String arg[]) { // scanner gets its input from the console. Scanner sc = new Scanner(System.in); String name = ""; System.out.print("Your name "); // Get the user's name. name = sc.next(); System.out.println(); // Print their name in a a message. System.out.println("Welcome, " + name + " to Javaland!"); } }
In this program uses Scanner class Method is use This class is pre defined class is define java itself and this class method uses in our class , to use import keyword and acess the class.
java is a package and util is a sub package and this package inside the class Scanner.
import java.util.*;
The * is a "regular expression operator" that will match any combination of characters. Therefore, this import statement will import everything in java.util.
If we need multiple classes from different packages, we use an import statement for each package from which we need to import classes (or interfaces, or any other part of that package we need.)
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*;
Previous | Home | Next |