06 Package
06 Package
PACKAGE IN JAVA.
DEFINE PACKAGE,
TYPES OF PACKAGE
NAMING CONVENTIONS
CREATING PACKAGES,
ACCESSING PACKAGE,
IMPORT STATEMENT,
STATIC IMPORT,
ADDING CLASS & INTERFACES TO A PACKAGE
CONCEPT OF PACAKGE:
You can think of packages as being similar to different folders on your computer.
o One folder might contains HTML pages.
o Second folder might contains images.
o Third folder might contains scripts or applications.
And all these folders contents accessed without physical copy it in our java programme.
INTRODUCTION OF PACKAGE.
A package is a namespace that organizes a set of related classes and interfaces.
Software written in the Java programming language can be composed of hundreds or thousands of individual
classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
Defination:
“Java provides a mechanism for partitioning the class namespace into more manageable parts. This mechanism is called
as “package‟. The package is both naming and visibility controlled mechanism”.
Some Points about Package.
Packages are used for grouping a variety of classes & interfaces together.
The grouping of package is done according to functionality.
Packages acts as containers of classes.
Packages are stored in hierarchical manner & are explicitly imported into new class definitions.
Syntax:
package packagename; Here, packagename is the name of the package.
ACCESSING PACKAGES
Syntax: import pkg1[.pkg2].(classname.*);
Save the AccessClass.java file in working directory i.e. outside the package folder.
D:\Galaxy \ AccessClass.java
Create the object of class (TestClass), which is store in package folder and access it.
Complete Defination of class is as follow
public class AccessClass
{
public static void main( String args[] )
{
TestClass A = new TestClass();
A. test_Method1(); // Accessible
A. test_Method2(); // Not Accessible because weaker access
}
}
Static Import
Static import is a feature which allows members (fields and methods) defined in a class as public static to be used
in Java code without specifying the class in which the field is defined.
Example is as follow.
import static java.lang.System.out;
class TestABC
{
public static void main(String[] args)
{
out.println("Print Success …..");
}
}
Observe that due to static import of System.out in this program, we can use println method without System class
name.