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


5.3 Importing a package

When referring to a class or interface in a package you need to specify the package name in front of every class name and interface name in the package you want to access, like so, in the main folder ~/jtw-tutorials (outside of any package):

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

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

import pkg.*;

class B
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