Next: , Previous: , Up: Packages in J.T.W. and Java   [Contents][Index]


5.4 Importing a package from another package

When referring to a class or interface in a package you need to specify the package name in front of every class name or interface name in the package you want to access, like so, in the folder ~/jtw-tutorials/pkg (i.e.\ in the pkg package).

package pkg;

public class C
begin
    beginMain
        var pkg.inner.A a1 = new pkg.inner.A(123);
        a1.meth1(); // prints out "meth1:123"
        var pkg.inner.A a2 = new pkg.inner.A(456);
        a2.meth2(); // prints out "meth2:456"
        pkg.inner.A.func(); // prints out "func:666"
    endMain
end

To avoid having to qualify each class name or interface name with it’s package, you need to use the import directive like so after the package declaration but before the definition of the class or interface like so:

package pkg;

import pkg.inner.*;

public class C
begin
    beginMain
        var A a1 = new A(123);
        a1.meth1(); // prints out "meth1:123"
        var A a2 = new A(456);
        a2.meth2(); // prints out "meth2:456"
        A.func(); // prints out "func:666"
    endMain
end