Each file may have a package declaration which precedes all non-comment code. The package name must be the same as the enclosing directory.
Default package. If a package declaration is omitted, all classes in that directory are said to belong to the "default" package.
Here are two files in the packagetest directory.
package packagetest;and
class ClassA {
public static void main(String[] args) {
ClassB.greet();
}
}
package packagetest; // Same as in previous file.
class ClassB {
static void greet() {
System.out.println("Hi");
}
}
Note that these source files must be named ClassA.java and ClassB.java (case matters) and they must be in a directory named packagetest.
No comments:
Post a Comment