Next: How to build a collection of class files or an entire package, Previous: Importing a package, Up: Packages in J.T.W. and Java [Contents][Index]
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).
packagepkg;publicclassCbeginbeginMainvarpkg.inner.A a1 =newpkg.inner.A(123); a1.meth1(); // prints out "meth1:123"varpkg.inner.A a2 =newpkg.inner.A(456); a2.meth2(); // prints out "meth2:456" pkg.inner.A.func(); // prints out "func:666"endMainend
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:
packagepkg;importpkg.inner.*;publicclassCbeginbeginMainvarA a1 =newA(123); a1.meth1(); // prints out "meth1:123"varA a2 =newA(456); a2.meth2(); // prints out "meth2:456" A.func(); // prints out "func:666"endMainend