Consider a typical class:
// prints out "meth1:123"classAbeginpropertyint data;classVarint data2 = 666;constructorA(int d)begindata = d;endmethodvoidmeth1()beginSystem.out.println("meth1:" + data);endmethodvoidmeth2()beginSystem.out.println("meth2:" + data);endfunctionvoidfunc()beginSystem.out.println("func:" + data2);endbeginMainvarA a1 =newA(123); a1.meth1();// prints out "meth2:456"varA a2 =newA(456); a2.meth2();A.func();// prints out "func:666"endMainend
To move this class into a package called (for argument’s sake)
pkg, you need to set the class’s visibility status from none
(i.e. package visibility) to public. Also each package visible
(i.e. no private or public or protected specification) class variable,
function, method and property needs to have its visibility status
changed from package to public if you want to be able to access these
items from outside of the package. If you have more than one class in
the same file, they will have to be separated into separate files as
you can only have one public class per file. Also the name of the
package must be declared via a package specification like so
at the top of the file before any actual package pkg;class or
interface definitions. Here is the same source file, ready to be put
into a package:
packagepkg;publicclassAbeginpublicpropertyint data;publicclassVarint data2 = 666;publicconstructorA(intd)begindata = d;endpublicmethodvoidmeth1()beginSystem.out.println("meth1:" + data);endpublicmethodvoidmeth2()beginSystem.out.println("meth2:" + data);endpublicfunctionvoidfunc()beginSystem.out.println("func:" + data2);endbeginMainvarA 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
Also the source file for the class needs to be moved into the
folder ~/jtw-tutorials/pkg. To run the class, you will need to
invoke the Makefile command:
make clean pkg/A.run