Program to import the class A from package p1
In previous program, we have crated a package named "p1" and added class "A" in that package. Now in this program we use that package "p1" to import the class "A" in class "PackageExample".
import keyword is used to import built-in and user-defined packages into your java source file. There are 3 different ways to refer to class that is present in different package
- Using fully qualified name
- import the only class you want to use.
- import all the classes from the particular package
class MyDate extends java.util.Date{
// statement;
}
import java.util.Date;
class MyDate extends Date{
// statement;
}
import java.util.*;
class MyDate extends Date{
// statement;
}
Remember that, if there are both package and import statement in your source file then the import statement is after package statement.
In this program, we have imported the class A from package p1 usnig the import statement as below:
PROGRAM
import p1.A;
PROGRAM
import p1.A;
class PackageExample
{
public static void main(String args[])
{
A a1 = new A();
a1.dispA();
}
}
OUTPUT
C:\>javac PackageExample.java C:\>java PackageExample Inside from Class A