package Package1;
public class ClassA
{
                public void displayA()
                {
                                System.out.println("Class A");
                }
}

Name of the source file must be saved as ClassA.java and stored it in the subdirectory Package1. Now compile this java file with ClassA.class and it must be stored in the same directory. Now design a main method class to import that package.

import Package1.ClassA;
class PackageTest1
{
                public static void main(String args[])
                {
                                ClassA objectA=new ClassA();
                                objectA.displayA();
                }
}

Now compile the PackageTest1.java file and create PackageTest1.class. When the PackageTest1 program is run, java looks for the file PackageTest1.class and load its using something called class loader. Now the interpreter knows that it also needs the code in the file ClassA.class and load it’s as well.

JAVA 31

Advertisements