Professional Documents
Culture Documents
PUTTING
CLASSES
TOGETHER
INTRODUCTION
Definition: Packages are Java’s way of grouping a
variety of classes and/or interfaces together.
Java
double y = java.lang.Math.sqrt(x);
Same package
No Yes Yes Yes Yes
subclass
Different package
No No Yes Yes No
subclass
Different package
No No No Yes No
non-sub class
HIDING CLASSES
When we import a package using asterisk (*), all public classes are
imported.
We may like to hide these classes from accessing from outside of the
package. Such classes should be declared “not public”.
Example:
package p1;
public class X{ //public class, available outside
//body of X
}
class Y{ // not public, hidden
//body of Y
}
Here, the class Y which is not declared public is hidden from outside
of the package p1. This class can be seen and used only by other
classes in the same package.
STATIC IMPORT
Static import is another language feature introduced with the
J2SE 5.0 release.
This feature eliminates the need of qualifying a static member
with the class name.
Static import statement used to import static members from
classes and use them without qualifying the class name.
import static package-name.Subpackage-name .class-
name.staticmember;
(OR)
import static package-name.subpackage-name.class-
name.*;
Example: