Package
Package
package otherPackage1;
class otherClass { … }
package otherPackage2;
class otherClass { … }
Name Conflict 2
• Compiler will remain silent, unless we try to use otherClass.
• Then it will display an error message.
• In this situation we should use the full name:
import otherPackage1.*;
import otherPackage2.*;
class myClass {
…
otherPackage1.otherClass
…
otherPackage2.otherClass
…
}
Short versus Full References
• Short reference:
import java.util.*;
class MyClass extends Date { … }
• Full reference:
class MyClass extends java.util.Date { … }
• Only the public components in imported
package are accessible for nonsub-classes in
the importing code!
Example: Packages 1
• A package MyPack with one public class Balance. The class
has two same-package variables: public constructor and a
public show method.
• package MyPack;
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n; bal = b;
}
public void show() {
if (bal<0) System.out.print("-->> ");
System.out.println(name + ": $" + bal);
}
}
Example: Packages 2
• The importing code has access to the public class
Balance of the MyPack package and its two public
members:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
Balance test = new Balance("J. J. Jaspers",
99.88);
test.show();
}
}